-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_https.js
More file actions
executable file
·152 lines (127 loc) · 4.58 KB
/
entity_https.js
File metadata and controls
executable file
·152 lines (127 loc) · 4.58 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
var fs = require('fs');
var express = require('express');
var http = require('http');
var https = require('https');
var helmet = require('helmet');
var jwt = require('jsonwebtoken');
var loginRoutes = require('./src/routes/loginRoutes').loginRoutes;
var annoRoutes = require('./src/routes/annotationRoutes').annotationRoutes;
var createConnection = require('./src/controllers/dbConn.js').createConnection;
//var client;
const configData = require('./src/config/config.js');
const { app : {expressPort},db:{caPM,certPM,keyPM,stackCompose} } = configData;
var initialize = require('./src/controllers/userControllers.js').initialize;
const {requestLogger, errorLogger} = require('./src/controllers/loggerMiddleware.js');
console.log("Variable to resolve the path");
console.log("file path is "+__filename);
console.log("Dir Path is "+__dirname);
console.log("certPM is "+certPM);
console.log("keyPM is "+keyPM);
const app = express();
app.use(helmet({
frameguard: {
action: 'deny'
}
}));
app.use(express.json());
// 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) => {
//console.log(req.connection);
//console.log(req);
if (req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0] === 'JWT') {
jwt.verify(req.headers.authorization.split(' ')[1], 'RESTFULAPIs', (err, decode) => {
console.log("Logging error to check for the expiry");
console.dir(err,{"depth": null});
//console.log(err);
if (err) {
req.jwtid = undefined;
// pass the error to express.
next(err);
}
//console.log("DECODED VALUE IS *******************");
//console.log(decode);
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);
annoRoutes(app);
} catch(e) {
console.log("Error in routes "+e);
}
var httpsPort = expressPort;
const options = {
// openssl based certificates defined in the environment files. Update them based on the server in which the installation is performed
key : fs.readFileSync(keyPM,'utf8'),
cert: fs.readFileSync(certPM,'utf8'),
ca: fs.readFileSync(caPM,'utf8')
};
var msg = "Annotation Stack "
if ( process.env.RELEASE_NUM && process.env.RELEASE_MSG) {
msg = msg + process.env.RELEASE_MSG + " Release " + process.env.RELEASE_NUM;
}
//var server = app.listen(expressPort, async () => {
var server = https.createServer(options,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");
// 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(`${msg} is listening at 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 with friendly messages
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(`${msg} is running on port is running on port ${expressPort}`)
);