-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.js
126 lines (111 loc) · 3.8 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
var express = require('express');
var fs = require('fs');
var Q = require('q');
var MongoClient = require('mongodb').MongoClient;
var OAuthGithub = require('./server/OAuthGithub');
var SafeMongoConnection = require('./server/SafeMongoConnection');
var helpers = require('./server/helpers');
var app = express();
var port = process.env.port || 8080;
var productionMode = process.env.PRODUCTION_MODE || 'local';
var secondsBetweenRefresh = process.env.SECONDS_BETWEEN_REFRESH || 120;
var mongoConnectionUrl = process.env.CUSTOMCONNSTR_MONGO_URI || 'mongodb://localhost/javascriptBattle';
var mongoOptions = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
},
auto_reconnect: true
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
}
};
var safeMongoConnection = new SafeMongoConnection(mongoConnectionUrl, mongoOptions);
safeMongoConnection.connect()
.done(
// If all goes well, app starts after DB is connected
function() {
// Serve up files in public folder
app.use('/', express.static(__dirname + '/public'));
// must serve up ejs files individually for Azure to accept in deployment
app.get('/ejs_templates/:ejsTemplate', function(req, res) {
// file server
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(fs.readFileSync(__dirname+'/public/ejs_templates/' + req.params.ejsTemplate + '.ejs'));
});
// Add github authentication
OAuthGithub(app, safeMongoConnection);
// The router for the API
var router = express.Router();
// Returns the state of the game on the given day and turn
router.get('/gameDataForUser/:turn', function(req, res) {
// If there is no user logged in, default to today's first game
var gameId = '0|' + helpers.getDateString(0, productionMode) + '|' + req.params.turn
// Otherwise, use the most recent gameId of the user
if (req.user && req.user.mostRecentGameId) {
gameId = req.user.mostRecentGameId + '|' + req.params.turn;
}
safeMongoConnection.safeInvoke('jsBattleGameData', 'findOne', {
'_id': gameId
})
.done(
function(game) {
res.status(200).send(game);
},
function(err) {
res.status(500).send('Something went wrong!');
throw err;
}
);
});
// Returns the leaderboard data for the specified time period and stat
router.get('/leaderboard/:timePeriod/:stat', function(req, res) {
var timePeriod = req.params.timePeriod;
var stat = req.params.stat;
var id = timePeriod + '|' + stat;
// Get stat for leaderboard
safeMongoConnection.safeInvoke('leaderboard', 'findOne', {
'_id': id
})
// Return if success, throw err if failure
.done(
function(result) {
res.status(200).send(result);
},
function(err) {
res.status(500).send('Something went wrong!');
throw err;
}
);
});
// // Returns the current announcement
// router.get('/announcement', function(req, res) {
// safeMongoConnection.getConnection().then(function(db) {
// var collection = db.collection('general');
// return Q.ninvoke(collection, 'findOne', {
// 'type': 'announcement'
// }).then(function(announcementData) {
// delete announcementData._id;
// res.json(announcementData);
// })
// }).catch(function(err) {
// res.send(err);
// });
// });
// Set root route for app's data
app.use('/api', router);
app.listen(port);
console.log('Listening on port: ', port);
},
// If there is an error on DB connection, throw an error
function(err) {
throw err;
}
);
// for ServerSpec.js to work must export app
module.exports = app;