-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (36 loc) · 1.15 KB
/
index.js
File metadata and controls
44 lines (36 loc) · 1.15 KB
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
var flock = require('flockos');
var config = require('./config.js');
var express = require('express');
var fs = require('fs');
flock.setAppId(config.appId);
flock.setAppSecret(config.appSecret);
var app = express();
// Listen for events on /events, and verify event tokens using the token verifier.
app.use(flock.events.tokenVerifier);
app.post('/events', flock.events.listener);
// Read tokens from a local file, if possible.
var tokens;
try {
tokens = require('./tokens.json');
} catch (e) {
tokens = {};
}
// save tokens on app.install
flock.events.on('app.install', function (event) {
tokens[event.userId] = event.token;
});
// delete tokens on app.uninstall
flock.events.on('app.uninstall', function (event) {
delete tokens[event.userId];
});
// Start the listener after reading the port from config
var port = config.port || 8080;
app.listen(port, function () {
console.log('Listening on port: ' + port);
});
// exit handling -- save tokens in token.js before leaving
process.on('SIGINT', process.exit);
process.on('SIGTERM', process.exit);
process.on('exit', function () {
fs.writeFileSync('./tokens.json', JSON.stringify(tokens));
});