index.js
1.33 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
47
48
49
50
51
52
'use strict';
const express = require('express');
const shortid = require('shortid');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const routerInfo = require('./router-info');
const routerSMS = require('./router-sms');
const routerUSSD = require('./router-ussd');
const app = express();
function middlewareInit(req, res, next) {
res.locals.xid = shortid.generate();
next();
}
function middlewareCustomLog(req, res, next) {
if (req.url.indexOf('/favicon.ico') === 0) {
res.sendStatus(404);
return;
}
logger.info('Incoming request on HTTP command server', { ip: req.ip, url: req.url, xid: res.locals.xid });
next();
}
function middlewareCheckApikey(req, res, next) {
if (!req.query.apikey || (req.query.apikey !== config.http_command_server.apikey)) {
res.json({
status: 'NOT-OK',
error: 'INVALID_APIKEY',
message: 'Invalid apikey',
});
} else {
next();
}
}
app.use(middlewareInit);
app.use(middlewareCustomLog);
app.use(middlewareCheckApikey);
app.use('/info', routerInfo);
app.use('/sms', routerSMS);
app.use('/ussd', routerUSSD);
app.listen(config.http_command_server.listen_port, () => {
logger.info(`HTTP command server listeing on port ${config.http_command_server.listen_port}`);
});