-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
149 lines (121 loc) · 4.26 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
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
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var reload = require('reload');
var mongoose = require("mongoose");
var mongo = require("mongodb")
var docs = require("express-mongoose-docs");
var errors = require("./errors/errors");
var my_middleware = require("./middleware");
/* ROUTES */
var courses = require('./routes/courses');
var faculties = require('./routes/faculties');
var subjects = require('./routes/subjects');
var places = require('./routes/places');
var index = require('./routes/index');
var db_connection = 'mongodb://localhost/openyorku'
var args = require("minimist")(process.argv.slice(2))
//include winston
//include underscore
var my_conf = require('./config.json');
var app = express();
// all environments
app.set('port', process.env.PORT || my_conf.server_options.port);
app.configure(function() {
app.use(express.favicon(__dirname + '/public/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser(my_conf.server_options.cookie_parser_secret));
app.use(express.session());
app.use(app.router);
if (args['check_key'] || my_conf.development_options.ignore_api_key === false) {
app.use(my_middleware.permissions);
}
app.use(my_middleware.counter);
app.use(my_middleware.api_ify);
})
app.configure('test', function() {
//connect to different db during tests
//remove all existing data in the document so there's no conflicts.
//WHY MUST YOU MAKE ME DO THIS MONGO!!!!!!!!!!!!!!!
var count = 0;
console.log('TESTING')
function clearDB(total, fn) {
for (i in mongoose.connection.collections) {
removeDoc(i, total, fn)
}
}
function removeDoc(i, total, fn) {
mongoose.connection.collections[i].remove({}, function() {
count++;
console.log('removed all entries for ' + i + ' in db.')
if (count === total) {
fn()
}
}) //mongoose drop
}
mongoose.connect('mongodb://localhost/openyorku-testing', function(err) {
var total_dbs = Object.keys(mongoose.connection.collections).length;
clearDB(total_dbs, function() {
console.log('cleared all dbs!')
});
});
})
app.configure('development', function() {
console.log('DEVELOPMENT')
//var args = process.argv.slice(2);
if (process.env.MONGOHQ_URL) {
//if theres an mongohq_url variable set, use that as the url for the db.
console.log("database URL set : " + process.env.db_connection)
db_connection = process.env.MONGOHQ_URL
}
mongoose.connect(db_connection, function(err) {
if (!err) { console.log("connected to mongodb") } else { throw err; }
});
});
app.configure('production', function() {
console.log("PRODUCTION");
});
//create a proper error middleware,
//create a logger middleware,
//create a user authentication middleware, including API keys
docs(app, mongoose);
/* HOME */
app.get('/', index.index);
app.get('/help', index.index);
app.get('/env', index.env);
app.get('/err', index.provoke_error('duplicate'));
app.get('/add_key', index.add_api_key);
app.get('/time', index.time)
app.get('/list_keys', index.list_api_keys);
/* COURSES */
app.get('/courses', courses.list);
app.get('/courses/del', courses.clear_db);
app.put('/courses', courses.modify_course);
app.get('/courses/:code', courses.show_course);
app.post('/courses', courses.add_course);
/* FACULTIES */
app.get('/faculties', faculties.list);
/* SUBJECTS */
app.get('/subjects', subjects.list);
app.get('/subjects/collect', subjects.collect_courses);
app.get('/subjects/del', subjects.clear_db);
app.get('/subjects/:code', subjects.show_subject);
app.put('/subjects', subjects.modify_subject);
app.post('/subjects', subjects.add_subject);
/* PLACES */
app.get('/places', places.list('Place'));
app.get('/places/buildings', places.list('Building'));
app.get('/places/restaurants', places.list('Restaurant'));
app.get('/places/buildings/del', places.clear_db('Building'));
app.get('/places/restaurants/del', places.clear_db('Restaurant'));
app.get('/places/del', places.clear_db());
app.post('/places/restaurants', places.add_restaurant);
app.post('/places/buildings', places.add_building);
/* USERS */
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});