-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolors.js
More file actions
84 lines (68 loc) · 2.48 KB
/
Copy pathcolors.js
File metadata and controls
84 lines (68 loc) · 2.48 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
#!/usr/bin/env nodejs
var debug = true;
var serverport = 8080;
var http = require('http');
var url = require('url');
var os = require('os');
var mycolor = process.env.COLOR || 'white';
var hostname = os.hostname();
// 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 handleRequest(req, rsp) {
var burncpu = 0;
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);
}
}
var font_color = 'black';
if (mycolor == 'black') {
font_color = 'white';
}
rsp.statusCode = 200;
rsp.setHeader("Content-Type", "text/html; charset=utf-8");
rsp.setHeader("X-Processed-By", hostname);
rsp.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n \"http://www.w3.org/TR/html4/strict.dtd\">\n");
rsp.write("<HTML lang=\"en-US\">\n");
rsp.write("<HEAD>\n<TITLE>" + mycolor + " app</TITLE>\n");
rsp.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n</HEAD>\n");
rsp.write("<BODY style=\"color: " + font_color + "; background-color:" + mycolor + ";\">\n");
rsp.write("<H1>" + mycolor + " app</H1>\n");
rsp.write("<H2>" + hostname + "</H2>\n");
rsp.write("<div>" + mycolor + "</div>\n");
rsp.write("</BODY>\n");
rsp.write("</HTML>\n");
rsp.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);
});
});