-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
101 lines (92 loc) · 3.37 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
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
var util = require("util"),
crypto = require("crypto"),
fs = require("fs"),
qs = require("querystring"),
url = require("url"),
view = require("./lib/view"),
config = require("./config/config");
var http, server;
var getall = function(request, response, data, callback){
if(data.plugins.length > 0){
var next = data.plugins.pop();
if(config.plugins && config.plugins[next] && config.plugins[next].disable){
return getall(request, response, data, callback);
}
try{
var plugin = require("./plugins/" + next);
plugin.get(request, response, data, getall);
}catch(e){
console.log(e);
}
} else {
delete data.plugins;
view.send(request, response, data);
}
};
var onRequest = function(request, response){
request.params = url.parse(request.url, true);
var path = request.params.pathname.substr(1).split("/");
var name = path[0];
switch(name){
case "favicon.ico":
response.status = 404;
return view.send(request, response, config.strings.status404);
case "":
/* falls through */
case "index.html":
if(config.files && config.files.enable){
return view.file(request, response);
} else {
return view.send(request, response, config.strings.index);
}
break;
case "assets":
if(config.files && config.files.enable){
return view.file(request, response, {file: path[1]});
} else {
return view.send(request, response, config.strings.disabled, {status: 501});
}
break;
case "json":
/* falls through */
case "all":
if(config.token && request.params.query.token !== config.token){
return view.send(request, response, "Token required", {status: 404});
}
var ap = [];
for(var p in config.plugins){
if(config.plugins[p].all === undefined || config.plugins[p].all) ap.push(p) ;
}
return getall(request, response, { plugins: ap });
default:
if(config.token && request.params.query.token !== config.token){
return view.send(request, response, "Token required", {status: 404});
}
if(config.plugins && config.plugins[name] && config.plugins[name].disable){
return view.send(request, response, config.strings.disabled, {status: 501});
}
try{
var plugin = require("./plugins/" + name);
plugin.get(request, response, {}, function(request, response, data){
view.send(request, response, data);
});
}catch(e){
console.log(e);
response.status = 404;
view.send(request, response, config.strings.status404);
}
}
};
if(config.usehttps){
http = require("https");
server = http.createServer(config.ssloptions, onRequest);
} else {
http = require("http");
server = http.createServer(onRequest);
}
server.listen(config.port);
console.log("Started npsystats server listening on ", config.port);
process.on('SIGINT', function () {
console.log('SIGINT. Exiting.');
process.kill(process.pid);
});