index.js 2.92 KB
const MODULE_NAME = 'CALLBACK';

const express = require('express');
const uniqid = require('uniqid');

const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');

const morgan = require('morgan');
const moment = require('moment');
const rfs = require('rotating-file-stream');

const dumper = require('./dumper');
const apikeyChecker = require('./apikey-checker');
const handlerPrepaid = require('./handler-prepaid');
const handlerPostpaid = require('./handler-postpaid');

const baseFileName = 'logs/callback_access_log';

const app = express();

const listenPort = config.partner.callback && config.partner.callback.port;

if (!listenPort) {
    logger.warn(`${MODULE_NAME} BC903CB7: Unknown listen port. Please specified on config.partner.callback.port`);
    process.exit(1);
}

if (config.partner && config.partner.callback && config.partner.callback.trust_proxy) {
    logger.verbose(`${MODULE_NAME} 9774C2DB: Trusting proxy`, {
        trusted: config.partner.callback.trust_proxy,
    });

    app.set('trust proxy', config.partner.callback.trust_proxy);
}

app.use((req, res, next) => {
    res.locals.xid = uniqid();
    next();
});

morgan.token('xid', (req, res) => (res.locals.xid));

const accessLogFilenameGenerator = (time, index) => {
    if (!time) return baseFileName;

    return [
        baseFileName,
        moment(time).format('YYYY-MM-DD'),
        index || null,
    ].filter((item) => item)
        .join('.');
};

const accessLogStream = rfs.createStream(accessLogFilenameGenerator, {
    interval: '1d',
});

app.use(
    morgan(
        ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :xid',
        {
            stream: accessLogStream,
        },
    ),
);

app.use((req, res, next) => {
    const { xid } = res.locals;

    logger.info(`${MODULE_NAME} 78F5DD49: Got a request on callback listener`, {
        xid,
        ip: req.ip,
        method: req.method,
        url: req.url,
    });

    next();
});

app.use('/apikey/:apikey', apikeyChecker);
app.use('/apikey/:apikey', express.urlencoded({ extended: true }));
app.use('/apikey/:apikey', express.json());

app.use('/apikey/:apikey', dumper);
app.use('/apikey/:apikey/prepaid', handlerPrepaid);
app.use('/apikey/:apikey/TOPUP', handlerPrepaid);
app.use('/apikey/:apikey/:hitType', handlerPostpaid);

app.use('/', (req, res) => {
    const { xid } = res.locals;

    res.json({
        status: 'OK',
        error: null,
        ts: new Date(),
        message: 'Have you RTFM?',
        xid,
    });
});

app
    .listen(listenPort, () => {
        logger.info(`${MODULE_NAME} F702D60D: Callback listener ready to get request`, {
            listenPort,
        });
    })
    .on('error', (e) => {
        logger.warn(`${MODULE_NAME} B299BD82: Exception on EXPRESS SERVER`, {
            eCode: e.code,
            eMessage: e.message,
        });

        process.exit(1);
    });