-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_https.js
More file actions
executable file
·224 lines (193 loc) · 7.51 KB
/
entity_https.js
File metadata and controls
executable file
·224 lines (193 loc) · 7.51 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var fs = require('fs');
var express = require('express');
var http = require('http');
var https = require('https');
var helmet = require('helmet');
const jwt = require('jsonwebtoken');
const addRequestId = require('express-request-id')();
const morgan = require('morgan');
const spawn = require('child_process');
const schedule = require('node-schedule');
var path = require('path');
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 phenotypeRoutes = require('./src/routes/phenotypeRoutes').phenotypeRoutes;
var genepanelRoutes = require('./src/routes/genePanelRoutes').genepanelRoutes;
var varDiscRoutes = require('./src/routes/varDiscRoutes').varDiscRoutes;
var createConnection = require('./src/controllers/dbConn.js').createConnection;
// SV routes included
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, releaseNum} } = configData;
var displayVersion = process.env.CENTER_NAME || 'Dev ';
var initialize = require('./src/controllers/entityController.js').initialize;
var initializeLogLoc = require('./src/controllers/entityController.js').initializeLogLoc;
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(helmet({
frameguard: {
action: 'deny'
}
}));
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 {
console.log("Routes initialization process");
loginRoutes(app);
app.use(requestLogger);
indRoutes(app);
familyRoutes(app);
phenotypeRoutes(app);
importRoutes(app);
queryRoutes(app);
genepanelRoutes(app);
varDiscRoutes(app);
populationRoutes(app);
getRoutes(app);
queryRoutesSV(app);
} catch(err) {
console.log("Error in routes "+err);
}
var httpsPort = expressPort;
const privatekey = fs.readFileSync(privkey,'utf8');
const certificate = fs.readFileSync(cert,'utf8');
const ca = fs.readFileSync(certAuth,'utf8');
const options = {
key : privatekey,
cert: certificate,
ca: ca
};
var server = https.createServer(options,app).listen(httpsPort,async () => {
var host = server.address().address;
var port = server.address().port;
//server.setTimeout();
try {
console.log("Now we are in the create server location");
// check and setup database collections
client = await createConnection();
console.log("Created 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);
}
try {
var retVal = await initializeLogLoc();
} catch(err1) {
console.log("Error initializing log path for import "+err1);
}
// schedule job every 6 hours to perform sample sheet scan
schedule.scheduleJob('0 */4 * * *', function() {
//schedule.scheduleJob('*/5 * * * *', function() {
var parsercwd = path.join(__dirname,'src','parser');
var parseSheet = path.join(__dirname,'src','parser','parseSampleSheet.js');
//var subprocess = spawn.fork(parseSheet);
console.log("Callin samplesheet subprocess ");
var subprocess = spawn.fork(parseSheet,{'cwd':parsercwd,'env':process.env});
subprocess.on('error', (err) => {
console.log("Looks like error in creating fork process for parseSampleSheet");
console.log(err);
});
//console.log(subprocess);
});
console.log(`Individual Express app listening https://${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);
// catch the uncaught errors that weren't wrapped in a domain or try catch statement
// do not use this in modules, but only in applications, as otherwise we could have multiple of these bound
process.on('uncaughtException', function(err) {
// handle the error safely
console.log("Was there any uncaught exception that was caught here. ");
console.log(err)
})
app.get('/', (req, res) =>
res.send(`${displayVersion} Version ${releaseNum} - WiNGS API with Annotation versioning support is running on port ${expressPort}`)
);