forked from lexandera/Aardwolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (76 loc) · 2.52 KB
/
app.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
'use strict';
var argv = require('optimist').argv;
var fs = require('fs');
var config = require('./config/config.defaults.js');
if (argv['h']) { config.serverHost = argv['h']; }
if (argv['host']) { config.serverHost = argv['host']; }
if (argv['p']) { config.serverPort = argv['p']; }
if (argv['port']) { config.serverPort = argv['port']; }
if (argv['d']) { config.fileServerBaseDir = argv['d']; }
if (argv['file-dir']) { config.fileServerBaseDir = argv['file-dir']; }
if (argv['file-port']) { config.fileServerPort = argv['file-port']; }
if (argv['o']) { config.outputDir = argv['o']; }
if (argv['v']) { config.verbose = true; }
if (argv['white-list']){ config.whiteList = argv['white-list'].split(','); }
try {
/* Makes sure the path exists and gets rid of any trailing slashes. */
config.fileServerBaseDir = fs.realpathSync(config.fileServerBaseDir);
} catch (e) {
console.error(e.message);
process.exit(1);
}
Array.prototype.equals = function (otherArray) {
return !(this < otherArray || otherArray < this);
}
var ips = scanAvailableIPs();
if (config.serverHost && ips.indexOf(config.serverHost) < 0) {
console.error('Configured host', config.serverHost, 'is not valid. You don\'t have that IP');
console.error('Available IPs are:', ips);
process.exit(1);
}
if (!config.serverHost) {
if (ips.length > 1) {
console.log("Cannot decide which IP to use, please specify one of these");
ips.forEach(function(ip, i) {
console.log('[', i + 1, ']:', ip);
})
var prompt = require('prompt');
prompt.start();
prompt.get({name: 'selection', validator: /^\d{1}$/, empty: false}, function(err, result) {
var ip = ips[result.selection - 1];
console.log('Choosen option', ip);
config.serverHost = ip;
startApp();
})
} else {
config.serverHost = ips[0];
startApp();
}
} else {
startApp();
}
function scanAvailableIPs() {
var os = require('os');
var interfaces = os.networkInterfaces(),
ips = [];
for (var dev in interfaces) {
interfaces[dev].forEach(function(details){
if (details.family == 'IPv4' && details.address != '127.0.0.1') {
ips.push(details.address);
}
});
}
return ips;
}
function startApp() {
var server = require('./server/server.js');
server.run();
if (config.runDebugServer) {
var debugFileServer = require('./server/debug-file-server.js');
debugFileServer.run();
}
if (config.runOfflineRewriter) {
var rewriterServer = require('./server/rewriter-server.js');
rewriterServer.run();
}
}