-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
index.js
98 lines (83 loc) · 2.64 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
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
"use strict";
var helpers = require('./helpers');
var config = require('./config');
var client = config.redis.client();
var express = require('express');
var app = express();
// templating engine stuff (ugh)
var ECT = require('ect');
var ectRendererOptions = {
watch: false,
root: __dirname + '/views',
ext : '.ect',
gzip: true
};
var ectRenderer = ECT(ectRendererOptions);
app.set('view engine', 'ect');
app.engine('ect', ectRenderer.render);
// main route
app.get('/', function (req, res) {
var queryKeys = [ "times_indexed",
"created_at",
"destroyed_at",
"live_visitors",
"content",
"comments_count" ];
client.multi()
.mget(queryKeys)
.lrange("comments", 0, -1)
.exec( function (err,reply) {
if (err) {
res.send("Oh dear. Something went horribly wrong.");
console.log("Horrific redis load error on page request: " + err);
} else {
var queryVal = reply[0];
var comments = reply[1].map(JSON.parse);
var timesIndexed = parseInt(queryVal[0], 10);
var siteIsAlive = timesIndexed === 0;
var createdAt = new Date(queryVal[1]);
var destroyedAt = queryVal[2] ? new Date(queryVal[2]) : new Date();
var data = {
timesIndexed: timesIndexed,
createdAt: createdAt,
destroyedAt: destroyedAt,
timeAlive: helpers.timeAlive(createdAt, destroyedAt),
liveVisitors: queryVal[3],
content: queryVal[4],
commentsCount: queryVal[5],
comments: comments
};
if (siteIsAlive) {
// display alive template
res.render('alive', data);
// update live_visitors count
client.incr("live_visitors");
} else {
// header GONE
res.status(410);
// display a text status message with stats
res.type("text");
res.render('dead', data);
// update dead_visitors count
client.incr("dead_visitors");
}
}
});
});
// load comment processing routes from separate module
require('./comments')(app, client);
// allow all robot overlords (this is default, but more fun to be specific)
app.get('/robots.txt', function (req, res) {
res.type("text");
res.write("User-agent: ia_archiver\nDisallow: /\n\n");
res.write("User-agent: *\nDisallow:\n");
res.end();
});
// for status pings
app.get('/status', function (req, res) {
res.type("text");
res.send("OK");
});
var port = process.env.PORT || 3000;
console.log("Starting up on port " + port);
app.listen(port);