forked from edsu/wikistream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
174 lines (143 loc) · 4.46 KB
/
Copy pathapp.js
File metadata and controls
174 lines (143 loc) · 4.46 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// imports
var fs = require('fs'),
irc = require('./irc'),
sys = require('sys'),
http = require('http'),
path = require('path'),
redis = require('redis'),
_ = require('underscore'),
sio = require('socket.io'),
express = require('express');
// get the configuration
var configPath = path.join(__dirname, "config.json");
var config = JSON.parse(fs.readFileSync(configPath));
var app = module.exports = express.createServer();
var requestCount = 0;
// get the wikipedia shortnames sorted by their longname
var wikisSorted = [];
for (var chan in config.wikipedias) wikisSorted.push(chan);
wikisSorted.sort(function (a, b) {
w1 = config.wikipedias[a].long;
w2 = config.wikipedias[b].long;
if (w1 == w2) return 0;
else if (w1 < w2) return -1;
else if (w1 > w2) return 1;
});
// set up the web app
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(redirectOldPort);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function () {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.static(__dirname + '/public'));
});
app.configure('production', function () {
app.use(express.errorHandler());
app.use(express.static(__dirname + '/public', {maxAge: 60*15*1000}));
});
app.get('/', function (req, res){
res.render('index', {
title: 'wikistream',
wikis: config.wikipedias,
wikisSorted: wikisSorted,
stream: true,
trends: false
});
});
app.get('/commons-image/:page', function (req, res){
var path = "/w/api.php?action=query&titles=" +
encodeURIComponent(req.params.page) +
"&prop=imageinfo&iiprop=url|size&format=json";
var opts = {
headers: {'User-Agent': 'wikistream'},
host: 'commons.wikimedia.org',
path: path
};
http.get(opts, function (response) {
//res.header('Content-Type', 'application/json');
response.on('data', function (chunk) {
res.setHeader('Cache-Control', 'public, max-age=1000')
res.write(chunk);
});
response.on('end', function () {
res.end();
});
});
});
app.get('/trends/', function (req, res){
res.render('trends', {
title: 'wikistream daily trends',
stream: false,
trends: true
});
});
app.get('/about/', function (req, res){
res.render('about', {
title: 'about wikistream',
stream: false,
trends: false
});
});
// TODO: might be able to create one stats view that does all these?
stats = redis.createClient(),
app.get('/stats/users-daily.json', function (req, res){
stats.zrevrange(['users-daily', 0, 99, 'withscores'], function (e, r) {
res.send(zresults(r));
});
});
app.get('/stats/articles-daily.json', function (req, res){
stats.zrevrange(['articles-daily', 0, 99, 'withscores'], function (e, r) {
res.send(zresults(r));
});
});
app.get('/stats/articles-hourly.json', function (req, res){
stats.zrevrange(['articles-hourly', 0, 99, 'withscores'], function (e, r) {
res.send(zresults(r));
});
});
app.get('/stats/robots-daily.json', function (req, res){
stats.zrevrange(['robots-daily', 0, 99, 'withscores'], function (e, r) {
res.send(zresults(r));
});
});
app.listen(config.port);
// set up socket.io to stream the irc updates
var io = sio.listen(app);
var updateStream = irc.listen(config, function(message) {
io.sockets.emit('message', message);
});
io.configure('production', function () {
io.set('log level', 2);
});
// some proxy environments might not support all socketio's transports
io.set('transports', config.transports);
// little helper to package up zrevrange redis query results
function zresults(resp) {
results = []
for (var i=0; i < resp.length; i+=2) {
r = JSON.parse(resp[i]);
r['score'] = resp[i+1];
results.push(r)
}
return results;
}
/* this is only really needed on inkdroid.org where wikistream was initially
* deployed to inkdroid.org:3000 and cited there, which resulted
* in google using inkdroid.org:3000 as the canonical URL for wikistream
* this bit of middleware will permanently redirect :3000 requests that
* bypass the proxy to wikistream.inkdroid.org. Hopefully Google will
* update their index :-)
*/
function redirectOldPort(req, res, next) {
if (req.header('host') == 'inkdroid.org:3000'
&& ! req.header('x-forwarded-for')) {
res.redirect('http://wikistream.inkdroid.org' + req.url, 301);
} else {
next();
}
}