index.js 1.21 KB
const MODULE_NAME = 'CORE-CALLBACK';

const DEFAULT_LISTENER_FROM_CORE = 25613;

const express = require('express');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const middlewareCommon = require('../middlewares/common');
const sender = require('./sender');
const dumperReq = require('./dumper/req');
const matrix = require('../matrix');

const app = express();

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

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

app.use(middlewareCommon);
app.use(dumperReq);

app.use((req, res) => {
    matrix.core.received += 1;
    res.end('OK');

    const data = req.method === 'GET' ? req.query : req.body;
    sender(data, res.locals.xid);
});

const port = (config.listener && config.listener.core && config.listener.core.port)
    || DEFAULT_LISTENER_FROM_CORE;

const server = app.listen(port, () => {
    logger.info(`${MODULE_NAME} 0375DC4E: Listen from CORE callback`, { port: server.address().port });
}).on('error', (e) => {
    logger.error(`${MODULE_NAME} A90E42D5: Can not listen CORE callback on port ${port}. ${e.toString()}`);
    process.exit(1);
});