cancel-push-server.js
1.21 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
45
46
const express = require('express');
const logger = require('tektrans-logger');
const config = require('../config');
const resendDelay = require('./resend-delay');
const app = express();
function init() {
if (!config || !config.push_server || !config.push_server.apikey || !config.push_server.cancel || !config.push_server.cancel.url || !config.push_server.cancel.port) {
return;
}
app.listen(config.push_server.cancel.port, function () {
logger.info('Cancel server listening', {port: config.push_server.cancel.port});
});
}
init();
function isValidApikey(req, res, next) {
if (config.push_server && config.push_server.apikey && (req.params.apikey === config.push_server.apikey)) {
next();
}
else {
res.end('INVALID_APIKEY');
}
}
function cancelHandler(req, res) {
let task = req.body;
if (!task || !task.trx_id) {
logger.warn('PUSH-CANCEL: Invalid task');
res.end('INVALID_TASK');
return;
}
logger.verbose('PUSH-CANCEL: Got cancel request', {trx_id: task.trx_id});
resendDelay.cancel(task.trx_id);
}
app.use(express.json());
app.use('/apikey/:apikey', isValidApikey);
app.use('/apikey/:apikey/cancel', cancelHandler);