-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
111 lines (84 loc) · 3.28 KB
/
server.js
File metadata and controls
111 lines (84 loc) · 3.28 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
const PATH = require("path");
const FS = require("fs");
const EXPRESS = require("express");
const PORT = process.env.PORT || 8080;
var serviceUid = false;
if (FS.existsSync(PATH.join(__dirname, "service.json"))) {
serviceUid = JSON.parse(FS.readFileSync(PATH.join(__dirname, "service.json"))).uid;
}
var pioConfig = JSON.parse(FS.readFileSync(PATH.join(__dirname, "../.pio.json")));
exports.main = function(callback) {
try {
var app = EXPRESS();
app.configure(function() {
app.use(function(req, res, next) {
if (serviceUid) {
res.setHeader("x-service-uid", serviceUid);
}
return next();
});
app.use(EXPRESS.logger());
app.use(EXPRESS.bodyParser());
app.use(EXPRESS.methodOverride());
});
var requestCount = 0;
app.get("/_internal_status", function(req, res, next) {
if (!req.headers["x-auth-token"]) {
return next();
}
if (req.headers["x-auth-token"] !== pioConfig.config["pio.service"].config.internalStatusAuthToken) {
return next(new Error("'x-auth-token' is invalid"));
}
var payload = {
process: {
memoryUsage: process.memoryUsage()
},
server: {
requestCount: requestCount
}
};
return res.end(JSON.stringify(payload, null, 4));
});
app.get(/^\/(.+\.html)$/, function (req, res, next) {
var tplPath = PATH.join(__dirname, "www", req.params[0]);
return FS.readFile(tplPath, "utf8", function(err, template) {
if (err) return next(err);
template = template.replace(/\{\{\s*config.HF_LOGGER_HOST\s*\}\}/g, pioConfig.config["pio.service"].config.logger.web.host);
template = template.replace(/\{\{\s*config.HF_LOGGER_API_LOGGER\s*\}\}/g, pioConfig.config["pio.service"].config.logger.web.api.logger);
template = template.replace(/\{\{\s*config.HF_LOGGER_API_RECORD\s*\}\}/g, pioConfig.config["pio.service"].config.logger.web.api.record);
res.writeHead(200, {
"Content-Type": "text/html",
"Content-Length": template.length
});
return res.end(template);
});
});
mountStaticDir(app, /^\/(.*)$/, PATH.join(__dirname, "www"));
var server = app.listen(PORT);
console.log("open http://0.0.0.0:" + PORT + "/");
return callback(null, {
server: server,
port: PORT
});
} catch(err) {
return callback(err);
}
function mountStaticDir(app, route, path) {
app.get(route, function(req, res, next) {
var originalUrl = req.url;
req.url = req.params[0] || "index.html";
EXPRESS.static(path)(req, res, function() {
req.url = originalUrl;
return next.apply(null, arguments);
});
});
};
}
if (require.main === module) {
exports.main(function(err) {
if (err) {
console.error(err.stack);
process.exit(1);
}
});
}