app.js
2.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
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
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import logger from "../utils/logger/logger";
import appConfig from "../config/app_config";
import uuidv4 from 'uuid/v4';
import responseTime from 'response-time';
import ServiceData from '../common/ServiceData'
import ResponseReply from '../common/ResponseReply'
const app = express();
const TAG = "[Application] ";
export function initial() {
app.use(cors());
app.use(bodyParser.json({
limit: '50mb'
}));
app.use((req, res, next) => {
req.headers.__reqId = "[" + uuidv4() + "] ";
req.specialType = "request";
req.startTime = new Date().getTime(); // stamp time start inservice
logger.info(req, `${TAG}Call "${req.originalUrl}"`);
logger.info(req, `${TAG}:: Request Header :: ${JSON.stringify(req.headers)}`);
logger.info(req, `${TAG}:: Request Data :: ${JSON.stringify(req.body)}`);
next();
});
app.use(responseTime((req, res, time) => {
const totalTimeInService = time;
if (totalTimeInService > 100) {
logger.warning(req, TAG + "Total time in service [over 100ms] : ", Math.round(totalTimeInService), " ms")
} else {
logger.info(req, TAG + "Total time in service : ", Math.round(totalTimeInService), " ms")
}
}));
/**
* Show App Detail
*/
app.all("/app/version", (req, res) => {
res.send({
appName: appConfig.appName,
description: appConfig.description,
version: appConfig.version
})
});
}
export function addPostMethod(uri, processor) {
app.post(uri, async (req, res) => {
try {
const sv = new ServiceData();
sv.setData(req, res);
const createResult = await processor.process(sv);
res.send(ResponseReply.success(createResult));
} catch (error) {
logger.error(error + '')
res.send(ResponseReply.error(error));
}
});
}
export function addGetMethod(uri, process) {
}
export function startApp() {
const server = app.listen(appConfig.systemConfig.APP_START_PORT, () => {
logger.info(`${TAG}Starting ${appConfig.appName} (Version ${appConfig.version}) , Listening on port ${appConfig.systemConfig.APP_START_PORT} !`)
});
// Set timeout for server
server.setTimeout(1000 * 60 * 30);
}