index.js
2.92 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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);
});