-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (47 loc) · 1.52 KB
/
server.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// const fs = require("fs");
// var options = {
// key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
// cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
// };
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const socket = require("socket.io")
const io = socket(server);
app.use(express.static("public"));
server.listen(process.env.PORT, () => {
console.log("server listening on " + process.env.PORT);
});
io.on('connection', newConnection);
function newConnection(socket) {
console.log('new connection @ ', socket.id);
socket.on('mouse', mouseMessage);
function mouseMessage(data) {
socket.broadcast.emit('mouse', data);
// io.socket.emit('mouse', data); // global emit - includes client that sent data
// console.log(data);
}
}
//
const cmd = require('node-cmd');
const crypto = require('crypto');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const onWebhook = (req, res) => {
let hmac = crypto.createHmac('sha1', process.env.SECRET);
let sig = `sha1=${hmac.update(JSON.stringify(req.body)).digest('hex')}`;
if (req.headers['x-github-event'] === 'push' && sig === req.headers['x-hub-signature']) {
cmd.run('chmod 777 ./git.sh');
cmd.run('./git.sh', (err, data) => {
if (data) {
console.log(data);
}
if (err) {
console.log(err);
}
})
cmd.run('refresh');
}
return res.sendStatus(200);
}
app.post('/git', onWebhook);