-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtomorrow.js
67 lines (48 loc) · 1.28 KB
/
tomorrow.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
var http = require('http');
var redis = require('redis');
var fs = require('fs');
module.exports.start = function(opts) {
"use strict";
var client = redis.createClient(opts.redisPort || 6379);
opts.redisPass && client.auth(opts.redisPass);
http.createServer(function(request, response) {
var route = request.url;
var code = 404;
if(route === "/test") {
response.writeHead(200, {
"content-type" : "text/html"
});
return fs.createReadStream('./test.html').pipe(response);
}
if(route === "/fingerprint") {
response.writeHead(200, {
"content-type" : "text/javascript"
})
return fs.createReadStream('./fingerprint.js').pipe(response);
}
if(route.indexOf('/log/') === 0) {
var fingerprint = route.match(/^\/log\/(.*)/)[1];
code = 204;
if(fingerprint) {
client.pfadd('anonymous:uniques', fingerprint);
} else {
code = 400;
}
}
if(route === "/count") {
return client.pfcount('anonymous:uniques', function(err, count) {
if(err) {
count = -1;
}
response.writeHead(200, {
"content-type" : "application/json"
})
return response.end(JSON.stringify({
count: count
}))
})
}
response.writeHead(code);
response.end();
}).listen(opts.port || 8080)
}