-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (136 loc) · 4.99 KB
/
index.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
// =============================================================================
// BASE SETUP {{{
// =============================================================================
// call the packages we need
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var debug = require('debug')('app:main');
var moment = require('moment');
var config = require('./config.json');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
mongoose.connect(config.mongodbURL);
var Activity = require('./app/models/activity');
var ade = require('./app/ade/index.js')(Activity);
var roomfinder = require('./app/roomfinder/index.js')(Activity);
var testfinder = require('./app/testfinder/index.js')(Activity);
// =============================================================================
// }}}
// =============================================================================
// =============================================================================
// MAKE ROOM CACHE {{{
// =============================================================================
ade.refreshRoomsCache();
var lastRefresh = new Date();
// =============================================================================
// }}}
// =============================================================================
// =============================================================================
// ROUTES FOR OUR API {{{
// =============================================================================
// get an instance of the express Router
var router = express.Router();
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
debug("URI /api/calendar" + req.path + ' Requested.');
// If it's been 10 minutes
if(new Date() - lastRefresh >= 600000) {
ade.refreshRoomsCache();
lastRefresh = new Date();
}
// Allow CORS
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
// make sure we go to the next routes and don't stop here
next();
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.jsonp({message: 'Hooray! welcome to our api :)'});
});
router.route('/activities')
.get(function(req, res) {
Activity.find({}, {_id:0,__v:0}, function(err, activities) {
if (err)
res.send(err);
res.jsonp(activities);
});
});
router.route('/activities/:limit')
.get(function(req, res) {
Activity.find({}, {_id:0,__v:0}, function(err, activities) {
if (err)
res.send(err);
res.jsonp(activities);
}).limit(req.params.limit);
});
router.route('/activities/:limit/:skip')
.get(function(req, res) {
Activity.find({}, {_id:0,__v:0}, function(err, activities) {
if (err)
res.send(err);
res.jsonp(activities);
}).limit(req.params.limit)
.skip(req.params.skip);
});
router.route('/tests')
.get(function(req, res) {
testfinder.findNextTests(function(err, tests) {
if(err)
res.send(err);
res.jsonp(tests)
});
});
router.route('/tests/:promotion')
.get(function(req, res) {
testfinder.findNextTestsFor(req.params.promotion,
function(err, tests) {
if(err)
res.send(err);
res.jsonp(tests)
});
});
router.route('/refresh')
.get(function(req, res) {
ade.refreshRoomsCache();
debug('Database refresh requested.');
res.jsonp({message: 'Rooms refreshed!'});
});
router.route('/rooms')
.get(function(req, res) {
roomfinder.findRoomNow(function(err, rooms) {
if(err)
res.send(err);
res.jsonp(rooms)
});
});
router.route('/rooms/:date')
.get(function(req, res) {
roomfinder.findRoom(moment(req.params.date),
function(err, rooms) {
if(err)
res.send(err);
res.jsonp(rooms)
});
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api/calendar', router);
// =============================================================================
// }}}
// =============================================================================
// =============================================================================
// START THE SERVER {{{
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);
// =============================================================================
// }}}
// =============================================================================
// vim: fdm=marker