Skip to content

Commit

Permalink
token from env
Browse files Browse the repository at this point in the history
  • Loading branch information
olegakbarov committed Mar 7, 2017
1 parent 0dec08d commit 693fdbf
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 19 deletions.
120 changes: 120 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
root: true
extends: 'eslint:recommended'

parser: babel-eslint

rules:
brace-style:
- 2
- 1tbs
- allowSingleLine: true

comma-style: 2
# complexity:
# - 2
# - 5

consistent-this:
- 2
- that

curly:
- 2
- multi-line

dot-location:
- 2
- property

eqeqeq:
- 2
- smart

generator-star-spacing:
- 2
- after

guard-for-in: 2

indent:
- 2
- 2
- SwitchCase: 1

linebreak-style:
- 2
- unix

max-depth:
- 2
- 2

max-len:
- 2
- 80

max-params:
- 2
- 5

max-nested-callbacks:
- 2
- 3

max-statements:
- 2
- 15

no-lonely-if: 2

no-multiple-empty-lines:
- 2
- max: 1

no-nested-ternary: 2
no-self-compare: 2
no-unneeded-ternary: 2
no-unused-expressions: 2

no-use-before-define:
- 2
- nofunc

object-curly-spacing:
- 2
- always

operator-assignment:
- 2
- always

operator-linebreak: 2

padded-blocks:
- 2
- never

quote-props:
- 2
- as-needed

quotes:
- 2
- single
- avoid-escape

semi: 2

keyword-spacing: 2
space-before-blocks: 2
space-before-function-paren:
- 2
- never

space-in-parens: 2
spaced-comment: 2
wrap-iife:
- 2
- inside

wrap-regex: 2
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"body-parser": "^1.17.1",
"crypto": "0.0.3",
"eventsource": "^0.2.1",
"express": "^4.14.0"
"eventsource": "0.2.1",
"express": "4.14.0"
}
}
40 changes: 23 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env node */
/* eslint-disable no-console */
'use strict';

const EventEmitter = require('events');
Expand All @@ -13,11 +14,13 @@ app.set('port', process.env.PORT || 3000);

app.get('/', (req, res) => {
res.send('this works as expected!');
})
});

app.get('/webhook', (req, res) => {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === process.env.MESSENGER_VALIDATION_TOKEN) {
if (
req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === process.env.MESSENGER_VALIDATION_TOKEN
) {
console.log('[server.js] Validating webhook!');

res.status(200).send(req.query['hub.challenge']);
Expand All @@ -29,35 +32,40 @@ app.get('/webhook', (req, res) => {

class SSE extends EventEmitter {}
const proxyEmitter = new SSE();
proxyEmitter.setMaxListeners(1);
proxyEmitter.setMaxListeners(10);

app.post('/webhook', (req, res) => {
// if (data.object === 'page') {
proxyEmitter.emit('msg', req);
const data = req.body;

if (data.object === 'page') {
proxyEmitter.emit('msg', data);

// timeout here = 20sec
res.sendStatus(200);
// }
}
});

// forward messages down to subscribed clients
app.get('/eventsource', (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control':'no-cache',
'Connection': 'keep-alive'
Connection: 'keep-alive'
});

console.log('Client connected to eventsoruce');

// workaround for Heroku: https://devcenter.heroku.com/articles/request-timeout
setInterval(() => {
res.write('ping \n\n');
}, 1000);


proxyEmitter.on('msg', data => {
res.write(`event:msg\ndata: ${JSON.stringify(data)}\n\n`);
});

// console.log(res.socket);

res.socket.on('close', () => {
console.log('Client has left');
});
Expand All @@ -71,18 +79,16 @@ app.all('/*', (req, res) => {
});

function verifyRequestSignature(req, res, buf) {
var signature = req.headers["x-hub-signature"];
var signature = req.headers['x-hub-signature'];

if (!signature) {
// For testing, let's log an error. In production, you should throw an
// error.
console.error("Couldn't validate the signature.");
} else {
var elements = signature.split('=');
var method = elements[0];
var signatureHash = elements[1];
const elements = signature.split('=');
const method = elements[0];
const signatureHash = elements[1];

var expectedHash = crypto.createHmac('sha1', APP_SECRET)
let expectedHash = crypto.createHmac('sha1', process.env.APP_SECRET)
.update(buf)
.digest('hex');

Expand All @@ -92,4 +98,4 @@ function verifyRequestSignature(req, res, buf) {
}
}

app.listen(process.env.PORT || 5000)
app.listen(process.env.PORT || 5000);

0 comments on commit 693fdbf

Please sign in to comment.