-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathserver.js
43 lines (36 loc) · 1.21 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
const express = require('express')
const cmd = require('node-cmd')
const crypto = require('crypto')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json())
const verifySignature = (req, res, next) => {
const payload = JSON.stringify(req.body)
const hmac = crypto.createHmac('sha1', process.env.GITHUB_SECRET)
const digest = 'sha1=' + hmac.update(payload).digest('hex')
const checksum = req.headers['x-hub-signature']
if (!checksum || !digest || checksum !== digest) {
return res.status(403).send('auth failed')
}
return next()
}
// Github webhook listener
app.post('/git', verifySignature, (req, res) => {
if (req.headers['x-github-event'] == 'push') {
cmd.get('bash git.sh', (err, data) => {
if (err) return console.log(err)
console.log(data)
return res.status(200).send(data)
})
} else if (req.headers['x-github-event'] == 'ping') {
return res.status(200).send('PONG')
} else {
return res.status(200).send('Unsuported Github event. Nothing done.')
}
})
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
})
app.listen(process.env.PORT, () => {
console.log(`Your app is listening on port ${process.env.PORT}`)
})