-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (121 loc) · 3.85 KB
/
app.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require("dotenv").config();
//express
const express = require("express");
let app = express();
// Health check
require("./healthCheck")(app);
//config and routes
global.config = require("./config");
require("./config/globalVariable")();
let environmentData = require("./envVariables")();
if(!environmentData.success) {
log.warning("Server could not start . Not all environment variable is provided");
process.exit();
}
let router = require("./routes");
//required modules
const fileUpload = require("express-fileupload");
const bodyParser = require("body-parser");
const cors = require("cors");
var fs = require("fs");
var path = require("path");
var expressValidator = require('express-validator');
//To enable cors
app.use(cors());
app.use(expressValidator())
//health check
app.get("/ping", (req, res) => {
res.send("pong!");
});
app.use(fileUpload());
app.use(bodyParser.json({ limit: '50MB' }));
app.use(bodyParser.urlencoded({ limit: '50MB', extended: false }));
app.use(express.static("public"));
fs.existsSync("logs") || fs.mkdirSync("logs");
const serviceBaseUrl = process.env.APPLICATION_BASE_URL || "/assessment/";
const observationSubmissionsHtmlPath = process.env.OBSERVATION_SUBMISSIONS_HTML_PATH ? process.env.OBSERVATION_SUBMISSIONS_HTML_PATH : "observationSubmissions"
app.use(express.static(observationSubmissionsHtmlPath));
app.get(serviceBaseUrl+observationSubmissionsHtmlPath+"/*", (req, res) => {
let urlArray = req.path.split("/")
urlArray.splice(0,3)
res.sendFile(path.join(__dirname, "/public/"+observationSubmissionsHtmlPath+"/"+urlArray.join("/")));
});
//API documentation (apidoc)
if (process.env.NODE_ENV == "development" || process.env.NODE_ENV == "local") {
app.use(express.static("apidoc"));
if(process.env.NODE_ENV == "local") {
app.get("/apidoc", (req, res) => {
res.sendFile(path.join(__dirname, "/public/apidoc/index.html"));
});
} else {
app.get(serviceBaseUrl+"apidoc/*", (req, res) => {
let urlArray = req.path.split("/")
urlArray.splice(0,3)
res.sendFile(path.join(__dirname, "/public/apidoc/"+urlArray.join("/")));
});
}
}
// app.get(serviceBaseUrl+"web/*", function(req, res) {
// res.sendFile(path.join(__dirname, "/public/assessment/web/index.html"));
// });
app.get(serviceBaseUrl + "web2/*", function (req, res) {
res.sendFile(path.join(__dirname, "/public" + serviceBaseUrl + "web2/index.html"));
});
var bunyan = require("bunyan");
global.loggerObj = bunyan.createLogger({
name: "foo",
streams: [
{
type: "rotating-file",
path: path.join(__dirname + "/logs/" + process.pid + "-all.log"),
period: "1d", // daily rotation
count: 3 // keep 3 back copies
}
]
});
global.loggerExceptionObj = bunyan.createLogger({
name: "exceptionLogs",
streams: [
{
type: "rotating-file",
path: path.join(__dirname + "/logs/" + process.pid + "-exception.log"),
period: "1d", // daily rotation
count: 3 // keep 3 back copies
}
]
});
app.all("*", (req, res, next) => {
if(ENABLE_BUNYAN_LOGGING === "ON") {
loggerObj.info({
method: req.method,
url: req.url,
headers: req.headers,
body: req.body
});
}
if(ENABLE_DEBUG_LOGGING === "ON") {
log.info("-------Request log starts here------------------");
log.info(
"%s %s on %s from ",
req.method,
req.url,
new Date(),
req.headers["user-agent"]
);
log.info("Request Headers: ", req.headers);
log.info("Request Body: ", req.body);
log.info("Request Files: ", req.files);
log.info("-------Request log ends here------------------");
}
next();
});
//add routing
router(app);
//listen to given port
app.listen(config.port, () => {
log.info(
"Environment: " +
(process.env.NODE_ENV ? process.env.NODE_ENV : "development")
);
log.info("Application is running on the port:" + config.port);
});