index.js 1.33 KB
'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}`);
});