-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (34 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const express = require('express')
const bodyParser = require('body-parser')
const crypto = require('crypto');
const port = process.env.PORT || 3100;
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET || '12345';
const app = express()
var http = require('http').Server(app);
var io = require('socket.io')(http);
// Declare a json parser, which also verifies the sender
var jsonParser = bodyParser.json({
verify: (req, res, buf, encoding) => {
const calculatedSignature = `sha1=${crypto.createHmac('sha1', WEBHOOK_SECRET).update(buf, 'utf8').digest('hex')}`;
if (calculatedSignature !== req.header('X-StatEngine-Signature')) throw new Error('Invalid Signature');
}
})
// Define Routes
// simple page showing incidents
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
})
// incoming webhook route
app.post('/incoming-webhook', jsonParser, (req, res) => {
io.emit('webhook-fired', req.body);
console.dir('emitting')
res.sendStatus(204);
})
io.on('connection', function(socket){
console.log('a viewer connected');
});
// start listening
http.listen(port, err => {
if (err) throw err
console.log(`> Ready On Server http://localhost:${port}`)
})