index.js
1.21 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
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);
});