forked from mozilla-services/Dockerflow
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
60 lines (53 loc) · 1.58 KB
/
server.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
var dispatch = require('httpdispatcher');
var http = require('http');
var fs = require('fs');
var mozlog = require('mozlog');
mozlog.config({app: "dockerflow-demo"});
var log = mozlog("general");
var verfile = __dirname + "/version.json";
dispatch.onGet("/", (req, res) => {
res.writeHead(200, {"Content-Type":"text/plain"});
res.end("hello.")
});
// for service monitoring to make sure the
// service is responding and normal
dispatch.onGet("/__heartbeat__", (req, res) => {
fs.stat(verfile, (err) => {
if (err) {
res.writeHead(500, {"Content-Type":"text/plain"});
res.end("Could not find version file")
} else {
res.writeHead(200, {"Content-Type":"text/plain"});
res.end("OK")
}
});
});
// for load balancers to make sure the app is
// running
dispatch.onGet("/__lbheartbeat__", (req, res) => {
res.writeHead(200, {"Content-Type":"text/plain"});
res.end("OK")
});
dispatch.onGet("/__version__", (req, res) => {
fs.stat(verfile, (err, stats) => {
if (err) {
res.writeHead(404, {"Content-Type":"text/plain"})
res.end("version data not found");
} else {
res.writeHead(200, {"Content-Type":"text/json"});
var fstream = fs.createReadStream(verfile);
fstream.pipe(res)
}
});
});
// listen on the PORT env. variable
if (process.env.PORT) {
http.createServer((res, req) => {
dispatch.dispatch(res, req);
}).listen(process.env.PORT, ()=> {
// output to stdout in mozlog format
log.info("server", {msg: "listening", port: process.env.PORT})
});
} else {
log.error("server", {msg: "no PORT env var"});
}