forked from cylab-tw/raccoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
169 lines (156 loc) · 5.24 KB
/
server.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
RegExp.prototype.toJSON = RegExp.prototype.toString;
const debug = require('debug')('DICOM Server');
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const compress = require('compression');
//login
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('connect-flash');
const mongodb = require('./models/mongodb');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')({ session: session }); //eslint-disable-line @typescript-eslint/naming-convention
//
const fetch = require('node-fetch');
const os = require('os');
const { exec } = require('child_process');
require('dotenv').config();
const port = process.env.SERVER_PORT;
const app = express();
require('rootpath')();
debug('Start DICOM Server...');
app.use(compress());
app.use(flash());
app.use(express.static('public'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json({
"strict": false,
"limit": "50mb"
}));
app.use(bodyParser.json({
"type": "application/fhir+json",
"limit": "50mb"
}));
app.use(bodyParser.text({ "type": "text/*" }));
//app.use(bodyParser.raw({ "type" : "multipart/related" , limit: "100gb"}));
app.use((err, req, res, next) => {
// This check makes sure this is a JSON parsing issue, but it might be
// coming from any middleware, not just body-parser:
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
console.error(err);
return res.status(400).json(require('./models/FHIR/httpMessage').handleError.exception(err.message)); // Bad request
}
next();
});
app.use(cookieParser());
//login
app.use(session({
secret: 'micalasecret',
resave: true,
saveUninitialized: true,
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
httpOnly: true
}));
//
//app.use(expressValidator());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept ,Authorization, apikey, user-agent");
res.header("Vary", "Origin");
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Expose-Headers", "Content-Disposition");
next();
});
//require('routes')(app);
//login
require("routes.js")(app);
app.engine('html', require('ejs').renderFile);
app.set("views", "public");
// If host is present, the server will accept connection on specific host, when
// host is omitted, use IPv6 (::) or IPv4 (0.0.0.0)
if (process.env.SERVER_HOST) {
http.createServer(app).listen(port, process.env.SERVER_HOST, function () {
console.log(`http server is listening on port:${port}`);
});
} else {
http.createServer(app).listen(port, function () {
console.log(`http server is listening on port:${port}`);
});
}
let osPlatform = os.platform().toLocaleLowerCase();
if (osPlatform.includes('linux')) {
process.env.ENV = "linux";
} else if (osPlatform.includes('win')) {
process.env.ENV = "windows";
}
process.env.USE_DCM2JPEG_PYTHONAPI = (process.env.USE_DCM2JPEG_PYTHONAPI=='true') ? true : false;
let condaPath = process.env.CONDA_PATH;
let condaEnvName = process.env.CONDA_GDCM_ENV_NAME;
(async ()=> {
let isflaskRunning = false;
if (process.env.USE_DCM2JPEG_PYTHONAPI) {
try {
let res = await fetch(`http://${process.env.DCM2JPEG_PYTHONAPI_HOST}:${process.env.DCM2JPEG_PYTHONAPI_PORT}/`);
let resJson = await res.json();
if (resJson.status) {
isflaskRunning = true;
console.log('the dcm2jpeg python flask api ready');
}
} catch (e) { // eslint-disable-line no-empty
}
if (!isflaskRunning) {
if (process.env.ENV == "windows") {
let cmd = `python DICOM2JPEGAPI.py ${process.env.DCM2JPEG_PYTHONAPI_PORT}`;
if (process.env.USE_CONDA === "true") cmd = `${condaPath} run -n ${condaEnvName} python DICOM2JPEGAPI.py ${process.env.DCM2JPEG_PYTHONAPI_PORT}`;
exec(`${cmd}`, {
cwd: process.cwd()
}, function (err, stdout, stderr) {
if (err) {
console.error(err);
process.exit(1);
} else if (stderr) {
console.error(stderr);
process.exit(1);
}
});
} else {
exec(`python3 DICOM2JPEGAPI.py ${process.env.DCM2JPEG_PYTHONAPI_PORT}`, {
cwd: process.cwd()
}, function (err, stdout, stderr) {
if (err) {
console.error(err);
process.exit(1);
} else if (stderr) {
console.error(stderr);
}
});
}
}
}
})();
(()=> {
let checkAPITimes = 0;
if (process.env.USE_DCM2JPEG_PYTHONAPI) {
let checkAPIInterval = setInterval(async ()=> {
if (checkAPITimes >=30) {
console.error("The dcm2jpeg python flask api set up failure");
process.exit(1);
}
try {
let res = await fetch(`http://${process.env.DCM2JPEG_PYTHONAPI_HOST}:${process.env.DCM2JPEG_PYTHONAPI_PORT}/`);
console.log('the dcm2jpeg python flask api ready');
clearInterval(checkAPIInterval);
} catch (e) {
checkAPITimes++;
}
} , 1000);
}
})();
module.exports = app;