-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
118 lines (100 loc) · 2.89 KB
/
app.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
/**
* Multiplayer shooting game
*
*/
// Dependences
var express = require('express'),
config = require('./config'),
fs = require('fs'),
mongoose = require('mongoose'),
MongoStore = require('connect-mongo')(express),
io = require('socket.io'),
cookie = require('cookie');
// Init express & Socket.io
var app = express();
var server = app.listen(config.port);
var sio = io.listen(server);
//io.setNoDelay(true);
sio.set('log level', 1);
// Bootstrap db connection
var db = mongoose.connect(config.db, { auto_reconnect: true });
// Set up mongoStore
var mongoStore = new MongoStore({
db : db.connection.db,
collection : config.sessionCollection
});
// socket.io authorization handler (handshake)
sio.set('authorization', function (data, callback) {
if(!data.headers.cookie) {
// Testing
data.session = { user: {} };
return callback(null, true);
// return callback('No cookie transmitted.', false);
} else {
data.cookie = cookie.parse(data.headers.cookie);
if(data.cookie[config.sessionKey]){
var sessionID = data.cookie[config.sessionKey].split(":");
sessionID = sessionID[1].split(".");
sessionID = sessionID[0];
// Get session from mongoStore
mongoStore.get(sessionID, function(err, session){
if(session){
data.session = session;
callback(null, true);
} else {
callback('Not logged in', false);
}
});
} else {
callback('Not logged in', false);
}
}
});
// Express configuration
if (app.get('env')) {
app.set('views', __dirname + '/app/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
// Express/Mongo session storage
app.use(express.session({
key : config.sessionKey,
secret : config.sessionSecret,
store : mongoStore
}));
app.use(app.router);
}
if (app.get('env')) {
app.use(express.errorHandler());
}
var walk = function(path, _app) {
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (stat.isFile()) {
if (/(.*)\.(js$|coffee$)/.test(file)) {
if(_app){
require(newPath)(_app);
} else {
require(newPath);
}
}
} else if (stat.isDirectory()) {
walk(newPath);
}
});
};
// Bootstrap models
var models_path = __dirname + '/app/models';
walk(models_path, null);
// Bootstrap routes
var routes_path = __dirname + '/app/routes';
walk(routes_path, app);
// Socket.io
sio.sockets.on('connection', function(socket) {
require('./app/socket_events')(sio, socket);
});
console.log(config.gameTitle + ' server listening on port ' + config.port);