forked from bedis/colors_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.js
158 lines (130 loc) · 4.06 KB
/
colors.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env nodejs
var http = require('http');
var url = require('url');
var os = require('os');
var fs = require('fs');
var hostname = os.hostname();
var debug = true;
var serverport = process.env.PORT || 8080;
var mycolor = process.env.COLOR || 'yellow';
var readyRequestCount = process.env.READY_COUNT || 5;
var liveRequestCount = process.env.LIVE_COUNT || 5;
var font_color = 'black';
if (mycolor == 'black') {
font_color = 'white';
}
// https://gist.githubusercontent.com/joelpt/3824024/raw/df31dca35b84ff3f2a4a4d8bd21606ae8c671bdd/squirt.js
// The Babylonian Method
// http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
// @param n - the number to compute the square root of
// @param g - the best guess so far (can omit from initial call)
function squirt(n, g) {
if (!g) {
// Take an initial guess at the square root
g = n / 2.0;
}
var d = n / g; // Divide our guess into the number
var ng = (d + g) / 2.0; // Use average of g and d as our new guess
if (g == ng) {
// The new guess is the same as the old guess; further guesses
// can get no more accurate so we return this guess
return g;
}
// Recursively solve for closer and closer approximations of the square root
return squirt(n, ng);
}
function delay(time) {
setTimeout(function () {
console.log('This printed after about ' + time);
}, time);
}
var rIdx = 0;
function handleReady(req, res) {
console.log((new Date()) + ' Received ready request ' + rIdx + ' of ' + readyRequestCount);
if (rIdx < readyRequestCount) {
rIdx += 1
delay(1000)
res.setHeader("Content-Type", "application/json");
res.writeHead(404);
res.end(`{"status": "not ready"}`);
return
}
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(`{"status": "ready"}`);
};
var lIdx = 0
function handleLive(req, res) {
console.log((new Date()) + ' Received live request ' + lIdx + ' of ' + liveRequestCount);
if (lIdx < readyRequestCount) {
lIdx += 1
delay(1000)
res.setHeader("Content-Type", "application/json");
res.writeHead(404);
res.end(`{"status": "still not alive"}`);
return
}
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(`{"status": "alive"}`);
}
function handleRequest(req, res) {
switch (req.url) {
case "/health/ready":
handleReady(req, res)
break
case "/health/live":
handleLive(req, res)
break
case "/":
handleIdx(req, res)
break;
default:
res.writeHead(404);
res.end('error:"Resource not found"');
}
}
function handleIdx(req, res) {
var query = url.parse(req.url, true).query;
if (debug) {
console.log((new Date()) + ' Received request for color ' + mycolor + ' on URL ' + req.url);
}
if (query.burncpu > 1) {
var x = 0.0001;
for (var i = 0; i < query.burncpu; i++) {
x = squirt(x);
}
}
const headerItemList = []
for (const headerItem of Object.keys(req.headers).sort()) {
headerItemList.push(`<tr><td>${headerItem}:</td><td>${req.headers[headerItem]}</td></tr>`);
}
const htmlFile = 'index.html';
res.statusCode = 200;
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.setHeader("X-Processed-By", hostname);
res.setHeader("X-Processed-Color", mycolor);
fs.readFile(__dirname + '/index.html', 'utf-8', function (error, data) {
if (error) {
console.log((new Date()) + ' error on URL ' + req.url + ': ' + error);
res.writeHead(404);
res.write('Whoops! File not found!');
} else {
data = data.replace(/\$background_color/g, mycolor);
data = data.replace(/\$font_color/g, font_color);
data = data.replace(/\$hostname/g, hostname);
data = data.replace(/\$headers/g, headerItemList.join('\n'))
res.write(data);
}
res.end();
})
}
var wsrv = http.createServer(handleRequest);
wsrv.listen(serverport, function () {
console.log("Server listening on port %s for color %s", serverport, mycolor);
});
process.on('SIGTERM', function () {
wsrv.close(function () {
process.exit(0);
});
});