forked from rakeshok/gameserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (40 loc) · 1.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
var assert = require('better-assert');
var fs = require('fs');
var path = require('path');
var socket = require('./server/socket');
var database = require('./server/database');
var Game = require('./server/game');
var Chat = require('./server/chat');
var GameHistory = require('./server/game_history');
var lib = require('./server/lib');
var _ = require('lodash');
var port = process.env.PORT || 3842;
var server;
if (process.env.USE_HTTPS) {
var options = {
key: fs.readFileSync(process.env.HTTPS_KEY || path.join(__dirname, '/key.pem')),
cert: fs.readFileSync(process.env.HTTPS_CERT || path.join(__dirname, 'cert.pem'))
};
server = require('https').createServer(options).listen(port, function() {
console.log('Listening on port ', port, ' on HTTPS!');
});
} else {
server = require('http').createServer().listen(port, function() {
console.log('Listening on port ', port, ' with http');
});
}
database.getGameHistory(function(err,rows) {
if (err) {
console.error('[INTERNAL_ERROR] got error: ', err,
'Unable to get table history');
throw err;
}
var gameHistory = new GameHistory(rows);
var game = new Game(gameHistory);
var chat = new Chat();
process.on('SIGTERM', function() {
console.log('Got SIGTERM... triggering emergency shutdown');
game.shutDownFast();
});
socket(server, game, chat);
});