-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (53 loc) · 1.67 KB
/
index.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
var http = require('http');
var https = require('https');
var Handlebars = require("handlebars");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var scrapeCatLogs = require('./catscrape');
function logDayToMessages(date) {
return scrapeCatLogs(date)
.then(function (logs) {
console.log('got logs');
return logs.split('\n').map(function(log) {
var msgRegex = /^\[(.*?)\] <(.*?)> (.*)$/
var result = log.match(msgRegex);
if (!result)
return null;
var logMessage = {
timestamp: result[1],
user: result[2],
text: result[3]
};
return logMessage;
}).filter(function(x) { return x });
});
}
function todayToHtml() {
var source =
["<table>"
,"{{#messages}}"
," <tr>"
," <td>{{timestamp}}</td>"
," <td>{{user}}</td>"
," <td>{{text}}</td>"
," </tr>"
,"{{/messages}}"
,"</table>"
].join('\n');
var template = Handlebars.compile(source);
return logDayToMessages(new Date())
.then(function(messages) {
console.log('got html');
var html = template({messages: messages});
return html;
}).catch(function(e) {
console.log(e);
});
}
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
console.log('req on ' + req.url);
todayToHtml().then(function(html) {
console.log('done');
res.end(html)
});
}).listen(process.env.PORT || 8181);