-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.js
More file actions
executable file
·166 lines (139 loc) · 5.12 KB
/
http_server.js
File metadata and controls
executable file
·166 lines (139 loc) · 5.12 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var fs = require('fs');
var express = require('express');
var http = require('http');
var https = require('https');
const jwt = require('jsonwebtoken');
const addRequestId = require('express-request-id')();
const morgan = require('morgan');
var loginRoutes = require('./src/routes/loginRoutes').loginRoutes;
var indRoutes = require('./src/routes/individualRoutes').indRoutes;
var familyRoutes = require('./src/routes/familyRoutes').familyRoutes;
var importRoutes = require('./src/routes/importRoutes').importRoutes;
var queryRoutes = require('./src/routes/queryRoutes').queryRoutes;
var createConnection = require('./src/controllers/dbConn.js').createConnection;
var populationRoutes = require('./src/routes/populationSVRoutes.js').populationRoutes;
var getRoutes = require('./src/routes/getRoutes.js').getRoutes;
var queryRoutesSV = require('./src/routes/queryRoutesSV.js').queryRoutesSV;
//const logger = require('./src/log/logger.js').logger;
const {requestLogger, errorLogger} = require('./src/controllers/loggerMiddleware.js');
const configData = require('./src/config/config.js');
const { app : {expressPort, privkey, cert, certAuth} } = configData;
var initialize = require('./src/controllers/entityController.js').initialize;
console.log("Variable to resolve the path");
console.log("file path is "+__filename);
console.log("Dir Path is "+__dirname);
const app = express();
// EXPRESS_PORT will be provided from environment file for docker setup
//const EXPRESS_PORT = 8081;
//The order of middleware loading is important: middleware functions that are loaded first are also executed first
app.use(express.json());
// START Morgan Logger
app.use(addRequestId);
morgan.token('id', function getId(req) {
return req.id
});
//var loggerFormat = ':id [:date[web]] ":method :url" :status :response-time';
var loggerFormat = ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]';
app.use(morgan(loggerFormat, {
skip: function (req, res) {
return res.statusCode < 400
},
stream: process.stderr
}));
app.use(morgan(loggerFormat, {
skip: function (req, res) {
return res.statusCode >= 400
},
stream: process.stdout
}));
// END Morgan Logger
// JWT setup
// If header authorization token is present in request, token is validated and jwtid is set
// If header is not present, jwtid will be reset.
// If loginRequired middleware is added to the endpoint, it checks for the jwtid
app.use((req, res, next) => {
if (req.headers && req.headers.authorization) {
var tokenIp = req.headers.authorization;
//if (req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0] === 'JWT') {
//jwt.verify(req.headers.authorization.split(' ')[1], 'RESTFULAPIs', (err, decode) => {
jwt.verify(tokenIp, 'RESTFULAPIs', (err, decode) => {
console.log(err);
console.log(decode);
if (err) {
console.log("Encountered error in JWT Verification");
req.jwtid = undefined;
// pass the error to express.
next(err);
}
console.log("JWT Decoded.Process to next function");
req.jwtid = decode;
next();
});
} else {
req.jwtid = undefined;
next();
}
});
// Specific endpoint in the route gets called based on the URL.
// Pass express app to Individual Routes
try {
loginRoutes(app);
app.use(requestLogger);
indRoutes(app);
familyRoutes(app);
importRoutes(app);
queryRoutes(app);
populationRoutes(app);
getRoutes(app);
queryRoutesSV(app);
} catch(err) {
console.log("Error in routes "+err);
}
var httpsPort = expressPort;
var server = http.createServer(app).listen(httpsPort,async () => {
var host = server.address().address;
var port = server.address().port;
//server.setTimeout();
try {
// check and setup database collections
await createConnection();
console.log("Creating Main Client Connection");
//console.log(client);
// Initializing database Collections
console.log("Calling initialize to create initial collections ");
var data = await initialize();
} catch (e) {
console.log("Error is "+e);
//process.exit(1);
}
console.log(`Individual Express app listening http://${host}:${port}`);
});
// Handle server errors
server.on('error', (error) => {
if (error.syscall !== 'listen') {
throw error;
}
port = expressPort;
const bind = typeof port === 'string'
? `Port ${port}`
: `Port ${port}`;
// handle specific listen errors
switch (error.code) {
case 'EACCES':
console.log(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.log(`${bind} is already in use`);
process.exit(1);
break;
default:
console.log(error);
// throw error;
}
});
// error-handling middleware should be defined last, after the other app.use and route calls.
app.use(errorLogger);
app.get('/', (req, res) =>
res.send('Node and express server is running on port '+expressPort)
);