-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
63 lines (55 loc) · 1.36 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* eslint-disable babel/new-cap, new-cap */
'use strict';
const app = require('express')();
const server = require('http').Server(app);
const bodyParser = require('body-parser');
const io = require('socket.io')(server);
const convict = require('convict');
const conf = convict({
port: {
doc: 'The port on which to listen for POSTs from the tracker.',
format: 'port',
default: 8080,
env: 'PORT',
arg: 'port'
},
secretKey: {
doc: 'The secret key that must be provided in POST requests for them to be accepted.',
format: String,
default: '',
env: 'SECRET_KEY',
arg: 'secretKey'
},
debug: {
doc: 'Whether or not to enable debug logging.',
format: Boolean,
default: false,
env: 'DEBUG',
arg: 'debug'
}
}).getProperties();
app.use(bodyParser.json());
server.listen(conf.port);
console.log(`Listening on port ${conf.port}.`);
app.get('/', (req, res) => {
res.send('Running OK');
});
// PayPal donations from the tracker are POSTed to us as they come in.
app.post(`/donation`, (req, res) => {
if (req.query.key !== conf.secretKey) {
res.sendStatus(403);
return;
}
if (conf.debug) {
console.log(req.body);
}
const data = {
name: req.body.donor__visiblename,
rawAmount: req.body.amount,
newTotal: req.body.new_total,
domain: req.body.domain
};
io.emit('donation', data);
console.log('Emitted donation:', data);
res.sendStatus(200);
});