index.js 2.22 KB
const MODULE_NAME = 'PARTNER-LISTENER';

const DEFAULT_LISTENER_FROM_PARTNER_PORT = 25614;

const express = require('express');

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

const middlewareCommon = require('../middlewares/common');

const routerInquiry = require('./routers/inquiry');
const routerPay = require('./routers/pay');
const routerTopup = require('./routers/topup');
const routerTrxStatus = require('./routers/trx-status');
const routerProductTree = require('./routers/product-tree');
const routerProductPrice = require('./routers/product-price');

const app = express();

if (config.listener && config.listener.partner && config.listener.partner.trust_proxy) {
    logger.info(`${MODULE_NAME} 01156CE1: Proxies trusted`, { trusted: config.listener.partner.trust_proxy });
    app.set('trust proxy', config.listener.partner.trust_proxy);
}

app.use((req, res, next) => {
    res.locals.httpgetx_subsystem = MODULE_NAME;
    next();
});

app.use(express.json({ extended: true }));
app.use(express.urlencoded({ extended: true }));
app.use(middlewareCommon);

app.use('/ping', (req, res) => {
    res.json({
        error: false,
        xid: res.locals.xid,
        from_ip: req.ip,
        ts: new Date(),
        request: {
            method: req.method,
            userAgent: req.get('user-agent'),
            contentType: req.get('content-type'),
            qs: req.query,
            body: req.body,
        },
    });
});
app.use('/inquiry', routerInquiry);
app.use('/pay', routerPay);
app.use('/topup', routerTopup);
app.use('/trx-status', routerTrxStatus);
app.use('/product-price', routerProductPrice);
app.use('/product-tree', routerProductTree);
app.use('/', routerTopup);

app.use((req, res) => {
    const { xid } = res.locals;
    res.status(404).end(`KOMODO-HTTP-GET-X CENTER.\n404: Method not found.\n\nXID: ${xid}.\n`);
});

app.listen(config.listener.partner.port || DEFAULT_LISTENER_FROM_PARTNER_PORT, () => {
    logger.info(`Listen from partner request on port ${config.listener.partner.port}`);
}).on('error', (e) => {
    logger.error(`Can not listen request from partner on port ${config.listener.partner.port || DEFAULT_LISTENER_FROM_PARTNER_PORT}. ${e.toString()}`);
    process.exit(1);
});