-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
386 lines (343 loc) · 10.4 KB
/
server.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* Module dependencies.
*/
var cluster = require('cluster')
, express = require('express')
, app = module.exports = express()
, http = require('http')
, server = http.createServer(app)
, session = require('express-session')
, npid = require('./lib/pid')
, async = require('async')
, sass = require('node-sass')
, mongoose = require('mongoose')
, everyauth = require('everyauth')
, request = require('request')
, cfg = require('config').cfg
, secrets = require('config').secrets
, airbrake = require('airbrake').createClient(secrets.airbrake_key, null, cfg.airbrake_host)
, Player = require('./models/player');
require('colors');
// var db = mongoose.createConnection('localhost', 'sizzlingstats');
mongoose.connect(cfg.mongo_url);
/**
* Exit cleanup stuff.
*/
var gracefullyExiting = false;
// Create a pidfile with the worker's ID and pid
try {
npid.create(__dirname + '/worker' +
(cluster.isWorker ? cluster.worker.id : '') +
'-' + process.pid + '.pid', true);
} catch (err) {
console.log(err);
process.exit(1);
}
// If gracefully exiting, prevent http keep-alive
app.use(function (req, res, next) {
if (gracefullyExiting) {
res.set('Connection', 'close');
}
next();
// res.send(502, 'The server is in the process of restarting.');
});
if (cluster.isWorker) {
process.on('message', function (message) {
if (message !== 'shutdown') { return; }
console.log('Worker', cluster.worker.id, 'is gracefully exiting...');
gracefulExit();
});
}
process.on('SIGTERM', gracefulExit);
process.on('SIGINT', exit);
// process.on('SIGKILL', exit); // Breaks in node v0.10.x
// This is just for removing the pidfile when nodemon is doing its thing
process.once('SIGUSR2', function() {
npid.remove(__dirname + '/worker' + (cluster.isWorker ? cluster.worker.id : '') +
'-' + process.pid + '.pid');
process.nextTick(function() {
process.kill(process.pid, 'SIGUSR2');
});
});
function gracefulExit () {
gracefullyExiting = true;
// TODO: Close socket.io connections gracefully, somehow
// Timeout idle connections after 3 seconds
// server.on('connection', function(socket) {
// socket.setTimeout(3*1000);
// });
server.close(function() {
mongoose.disconnect(function() {
exit(0);
});
});
// Forcefully shutdown after 6 seconds
setTimeout(function () {
console.error("Could not close connections in time, forcefully shutting down");
exit(1);
}, 6*1000);
}
function exit (code) {
process.nextTick(function() {
process.exit(code || 0);
});
}
/**
* Everyauth Configuration
*/
// Wait 8 seconds per step before timing out (default 10)
everyauth.everymodule.moduleTimeout(8000);
everyauth.everymodule.findUserById( function (req, userId, callback) {
Player.findById(userId, callback);
// callback has the signature, function (err, user) {...}
});
everyauth.everymodule.handleLogout( function (req, res) {
delete req.session.auth; // This is what req.logout() does
var that = this;
req.session.destroy(function() {
that.redirect(res, that.logoutRedirectPath());
});
});
everyauth.steam
.myHostname( cfg.address )
.findOrCreateUser( function (session, openIdUserAttributes) {
var promise = this.Promise();
var steamId, numericId;
try {
numericId = openIdUserAttributes.claimedIdentifier.split('/').slice(-1)[0];
if (!numericId) throw new Error('No steamid64???');
} catch (e) {
promise.fail(e);
return promise;
}
steamId = Player.numericIdToSteamId(numericId);
Player.findById(steamId, function(err, player) {
if (err) {
console.log('Error looking up player '+steamId, err);
console.trace(err);
return promise.fail(err);
}
if (player) {
// Update the player's info on login
// Instead of just retrieving old info
Player.getSteamApiInfo([numericId], function(err, steamInfo) {
if (err) return promise.fail(err);
player.name = steamInfo[0].personaname;
player.avatar = steamInfo[0].avatar;
player.updated = new Date();
if (steamInfo[0].loccountrycode) {
player.country = steamInfo[0].loccountrycode;
}
player.save(function(err) {
if (err) {
console.log('Error saving player', err);
console.trace(err);
return promise.fail(err);
}
promise.fulfill(player);
});
});
} else {
Player.getSteamApiInfo([numericId], function(err, steamInfo) {
if (err) return promise.fail(err);
var newPlayer = new Player({
_id: steamId
, numericid: numericId
, name: steamInfo[0].personaname
, avatar: steamInfo[0].avatar
, updated: new Date()
});
if (steamInfo[0].loccountrycode) {
newPlayer.country = steamInfo[0].loccountrycode;
}
newPlayer.save(function(err) {
if (err) {
console.log('Error saving new player', err);
console.trace(err);
return promise.fail(err);
}
promise.fulfill(newPlayer);
});
});
}
// session.save();
});
return promise;
})
.moduleErrback( function (err) {
console.log( 'EVERYAUTH ERROR:', err);
console.trace(err);
})
.redirectPath('/');
everyauth.debug = false;
/**
* Express Configuration
*/
// app.use(function(req, res, next) {
// console.log('Worker' + cluster.worker.id + 'is doing something');
// next();
// });
app.set('trust proxy', true);
app.use(express.limit('200kb'));
app.use(express.favicon(__dirname + '/public/img/favicon.png'
, { maxAge: 14 * 24 * 60 * 60 * 1000 } ));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {
layout: false
});
app.use(express.bodyParser());
app.use(express.methodOverride());
// Sessions
// app.store = new express.session.MemoryStore;
var RedisStore = require('connect-redis')(session);
app.store = new RedisStore({
prefix: cfg.session_prefix
, host: cfg.redis_host
, port: cfg.redis_port
, db: cfg.redis_db
, pass: cfg.redis_password
});
// app.use(express.cookieParser());
app.use(session({
proxy: true
, store: app.store
, secret: secrets.session
, cookie: {
path: '/'
, httpOnly: true
// cookies expire every 14 days
, maxAge: 14 * 24 * 60 * 60 * 1000
}
}));
app.use(everyauth.middleware());
// Asset Management
var assetManager = require('connect-assetmanager')({
js: {
route: /\/js\/all-[a-z0-9]+\.js/
, path: __dirname + '/public/js/'
, dataType: 'javascript'
, debug: app.get('env') === 'development'
, stale: app.get('env') === 'production'
, preManipulate: {
'^': [
function(src, path, index, isLast, callback) {
callback(src.replace(/#socketIoHostname#/g, cfg.socket_io_address));
}
, function(src, path, index, isLast, callback) {
callback(src.replace(/#socketIoPort#/g, cfg.socket_io_port));
}
// , function(src, path, index, isLast, callback) {
// if (/\.coffee$/.test(path)) {
// callback(coffee.compile(src));
// } else {
// callback(src);
// }
// }
]
}
, files: cfg.devScripts.concat([ // order matters here
'lib/modernizr.js'
, 'lib/typeahead.js'
, 'lib/foundation/foundation.js'
, 'lib/foundation/foundation.tooltip-5.1.1.js'
, 'lib/foundation/foundation.topbar.js'
, 'lib/foundation/app.js'
, 'app.js'
, 'services.js'
, 'controllers.js'
, 'controller-stats.js'
, 'controller-player.js'
, 'controller-settings.js'
, 'directive-stats.js'
, 'directive-typeahead.js'
// , '*'
])
},
css: {
route: /\/css\/all-[a-z0-9]+\.css/
, path: __dirname + '/public/css/'
, watchPath: true
, dataType: 'css'
, debug: app.get('env') === 'development'
, stale: app.get('env') === 'production'
, preManipulate: {
'^': [
function(src, path, index, isLast, callback) {
if (/\.scss$/.test(path)) {
sass.render({
file: path
}, function (err, result) {
if (err) {
console.log(err);
callback(err);
} else {
callback(result.css.toString());
}
});
} else {
callback(src);
}
}
]
}
, files: [ // order matters here
// 'foundation.css'
'app.scss'
, 'typeahead.css'
, 'sizzlingstats.scss'
]
}
});
app.use(assetManager);
// app.helpers({ assetManager: assetManager });
app.locals.assetManager = assetManager;
app.use(express.staticCache());
if (app.get('env') === 'development') {
app.use(express.static(__dirname + '/public'));
} else {
app.use(express.static(__dirname + '/public', {maxAge: 24 * 60 * 60 * 1000}));
}
app.use(app.router);
if (app.get('env') !== 'production') {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
} else {
// Airbrake
app.use(function(err, req, res, next) {
airbrake.expressHandler().apply(this, arguments);
});
// Error handler
app.use(function(err, req, res, next) {
res.set('Content-Type', 'text/plain');
res.send(500, "that's a meatshot" );
});
}
// Routes
require('./routes')(app);
// Hook Socket.io into Express
app.io = require('socket.io')(server);
// Redis adapter for socket.io
var socketRedisAdapter = require('socket.io-redis')
, socketRedis = require('socket.io-redis/node_modules/redis')
, pub = socketRedis.createClient(cfg.redis_port, cfg.redis_host, {return_buffers: true})
, sub = socketRedis.createClient(cfg.redis_port, cfg.redis_host, {return_buffers: true});
async.applyEach([ pub.select.bind(pub), sub.select.bind(sub) ]
, cfg.redis_db, function (err) {
if (err) {
console.log(err);
console.trace(err);
return false;
}
app.io.adapter( socketRedisAdapter({pubClient: pub, subClient: sub}) );
var socket = require('./routes/socket')(app);
});
/**
* Check status of Elasticsearch server
*/
request.get(cfg.elasticsearch_url + '/_status', function(err, res, body) {
if (err || res.statusCode !== 200) {
return console.log('WARNING: Elasticsearch index "sizzlingstats" not found.');
}
console.log('Elasticsearch index "sizzlingstats" found.');
});
module.exports = server;