-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathapp.js
82 lines (71 loc) · 2.83 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
const http = require('http');
const os = require('os');
const fs = require('fs');
const version = "0.1";
const listenPort = 8080;
function sendResponse(status, contentType, encoding, body, response) {
response.writeHead(status, {'Content-Type': contentType});
response.write(body, encoding);
response.end();
}
function renderFile(req, res, path, contentType) {
let template = fs.readFileSync(path, 'utf8');
let map = Object.assign({
"{{hostname}}": os.hostname(),
"{{clientIP}}": req.connection.remoteAddress,
"{{version}}": version,
});
let body = template.replace(
new RegExp(Object.keys(map).join('|'), 'g'),
function (matched) {
return map[matched];
});
sendResponse(200, contentType, 'utf8', body, res);
}
function sendFile(req, res, path, contentType) {
let body = fs.readFileSync(path, 'binary');
sendResponse(200, contentType, 'binary', body, res);
}
// this function guesses if the client that sent the request is a full-fledged
// graphical web browser and not a text-based tool such as curl
// graphical browsers typically send accept: text/html,application/xhtml+xml,...
// curl sends accept: */*
function isGraphicalWebBrowser(req) {
let accept = req.headers.accept || "*/*";
return accept.startsWith("text/html");
}
function handler(req, res) {
let clientIP = req.connection.remoteAddress;
console.log("Received request for " + req.url + " from " + clientIP);
switch (req.url) {
case '/':
if (isGraphicalWebBrowser(req)) {
res.writeHead(302, {"Location": "html"});
res.write("Redirecting to the html version...");
res.end();
return;
}
// text-based clients fall through to the '/text' case
case '/text':
return renderFile(req, res, "html/index.txt", "text/plain");
case '/html':
return renderFile(req, res, "html/index.html", "text/html");
case '/stylesheet.css':
return sendFile(req, res, "html/stylesheet.css", "text/css");
case '/javascript.js':
return sendFile(req, res, "html/javascript.js", "text/javascript");
case '/favicon.ico':
return sendFile(req, res, "html/favicon.ico", "image/x-icon");
case '/cover.png':
return sendFile(req, res, "html/cover.png", "image/png");
default:
return sendResponse(404, "text/plain", "utf8", req.url + " not found", res)
}
}
console.log("Kiada - Kubernetes in Action Demo Application");
console.log("---------------------------------------------");
console.log("Kiada " + version + " starting...");
console.log("Local hostname is " + os.hostname());
console.log("Listening on port " + listenPort);
let server = http.createServer(handler);
server.listen(listenPort);