This repository was archived by the owner on Oct 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (105 loc) · 3.25 KB
/
index.js
File metadata and controls
118 lines (105 loc) · 3.25 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
"use strict";
/*
Virtualization Layer | Main file
by Alexandre Alapetite http://alexandre.alapetite.fr
from Alexandra Institute http://www.alexandra.dk
for the ALMANAC European project http://www.almanac-project.eu
*/
Error.stackTraceLimit = 50;
var http = require('http'),
fs = require('fs'),
basicHttp = require('./basicHttp.js').basicHttp, //Static files, logs
config = require('./config.js').config,
almanac = require('./almanac.js').almanac;
if (fs.existsSync('./config.local.js')) { //Load local configuration, if any
var localConfig = require('./config.local.js').config,
extend = require('extend');
extend(true, config, localConfig);
}
almanac.config = config;
almanac.version = require('./package.json').version;
almanac.log = require('npmlog');
almanac.log.level = almanac.config.logLevel;
basicHttp.npmlog = almanac.log;
almanac.basicHttp = basicHttp;
almanac.http = http;
var server = http.createServer(function (req, res) {
try {
basicHttp.log(req, null); //Verbose logging
} catch (ex) {
almanac.log.error('VL', 'Node.js: Log exception: %s', ex);
}
var reqUrl0 = ''; //Original request URL
function requestServed() {
try {
req.url = reqUrl0;
basicHttp.log(req, res);
} catch (ex) {
almanac.log.error('VL', 'Log exception: %s', ex);
}
}
var asyncRequest = false;
try {
if (res) {
res.on('error', function (err) {
almanac.log.error('VL', 'Internal response error: ' + err);
});
}
if (req && req.url) {
reqUrl0 += req.url;
var urlSegments = req.url.split('/', 3),
s1 = '';
switch (urlSegments.length) {
case 3:
s1 = urlSegments[1] + '/';
break;
case 2:
s1 = urlSegments[1];
break;
}
if (almanac.routes[s1]) {
req.url = req.url.substring(s1.length + 1);
if (config.requireAuthorization && !almanac.openPaths[reqUrl0]) {
asyncRequest = true;
almanac.jwtVerifyAuthorization(req, res, function (err, jwt) {
if (!err) {
almanac.routes[s1](req, res);
}
requestServed();
});
} else {
almanac.routes[s1](req, res);
}
} else if (almanac.openRoutes[s1]) {
req.url = req.url.substring(s1.length + 1);
almanac.openRoutes[s1](req, res);
} else if (s1 === '') {
req.url = '/index.html';
basicHttp.serveStaticFile(req, res);
} else {
basicHttp.serveStaticFile(req, res);
}
} else {
basicHttp.serve400(req, res);
}
} catch (ex) {
req.url = reqUrl0;
almanac.log.error('VL', 'Exception: ' + ex);
basicHttp.serve500(req, res, 'Exception: ' + ex);
}
if (!asyncRequest) {
requestServed();
}
});
server.on('error', function (err) {
almanac.log.error('VL', 'Node.js: server error: %s. Check that you can use port %d.', err.errno || err, config.hosts.virtualizationLayer.port);
process.exit(1);
});
server.on('connection', function (socket) {
socket.setNoDelay(true); //Disable Nagle's algorithm with TCP_NODELAY
var remoteAddress = socket.remoteAddress; //To populate ._peername https://github.com/joyent/node/blob/03e9f84933fe610b04b107cf1f83d17485e8906e/lib/net.js#L563 (e.g. for WebSocket)
});
almanac.server = server;
server.listen(config.hosts.virtualizationLayer.port);
almanac.init();
almanac.log.warn('VL', 'Node.js: server running ALMANAC Virtualization Layer at %j', server.address());