-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
73 lines (63 loc) · 2.34 KB
/
index.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
var http = require('http');
var httpProxy = require('http-proxy');
var static = require('node-static');
const path = require('path');
const fs = require('fs');
const system = require('./system');
const lookup = require('./lookup');
const archive = require('./archive');
const server = JSON.parse(fs.readFileSync('server.json'));
var front = new (static.Server)(path.join(__dirname, 'front'));
var proxy = httpProxy.createProxyServer({});
proxy.on('error', function (error, _req, res) {
if (!res.headersSent) {
res.writeHead(500, { 'content-type': 'application/json' });
}
res.end(JSON.stringify({
error: 'proxy_error',
reason: error.message
}));
});
if (typeof server.header !== 'undefined') {
if (typeof server.header.name !== 'undefined' && typeof server.header.value !== 'undefined') {
proxy.on('proxyReq', function (proxyReq, _req, _res, _options) {
proxyReq.setHeader(server.header.name, server.header.value);
});
} else {
console.log("Invalid header name or value in server.json file.");
return;
}
}
if (typeof server.proxy === 'undefined') {
console.log("Missing proxy URL in server.json file.");
return;
} else {
console.log("Proxy URL: " + server.proxy);
}
const port = (typeof server.port === 'undefined') ? 3000 : server.port;
const isProteus = (typeof server.proteus === 'undefined') ? false : true;
http.createServer(function (req, res) {
let chunks = req.url.split('/');
if (chunks[1] === 'dicom-web') {
if (chunks[chunks.length-1].split('?')[0]=="rendered") {
req.url = req.url.split('?')[0];
proxy.web(req, res, { target: server.proxy });
} else {
// Proxy WADO server
proxy.web(req, res, { target: server.proxy });
}
} else if (req.url === '/system') {
// Simulate Orthanc PACS response
system(server, req, res);
} else if (isProteus && req.url === '/tools/lookup') {
// Translate /tools/lookup (for Proteus PACS)
lookup(server, req, res);
} else if (isProteus && chunks[1] === 'studies') {
// Translate /studies/*/archive (for Proteus PACS)
archive(server, req, res);
} else {
// Use static local files
front.serve(req, res);
}
}).listen(port);
console.log("Listening on port " + port + ".");