-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
443 lines (405 loc) · 13.6 KB
/
web.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Define routes for simple SSJS web app.
// Writes Coinbase orders to database.
// Login strategies and mongoose initialization inspired by Sudeep Juvekar's repository: https://github.com/sjuvekar/3Dthon
// Persistent sessions inspired by http://stackoverflow.com/questions/7990890/how-to-implement-login-auth-in-node-js
// use correct environment
var env = require('node-env-file');
env(__dirname + '/.env');
// IMPORT STATEMENTS
var async = require('async')
, express = require('express')
, fs = require('fs')
, http = require('http')
, https = require('https')
// , db = require('./models')
, passport = require('passport')
, facebookAuth = require("./auth/facebook")
, twitterAuth = require("./auth/twitter")
, googleAuth = require("./auth/google")
, flash = require('connect-flash')
, mongooseDB = require('./models/mongooseDB')
, User = require('./models/user')
, Problem = require('./models/problem')
, Edit = require('./models/edit')
, Field = require('./models/field')
, Section = require('./models/section')
, FAQ = require('./models/faq')
, CarouselItem = require('./models/carouselitem')
, SingleEdit = require('./models/singleEdit')
, NewProblem = require('./models/newProblem')
, NewSection = require('./models/newSection')
, NewFAQ = require('./models/newFAQ')
, NewCarouselItem = require('./models/newCarouselItem')
, NewField = require('./models/newField')
, MongoStore = require('connect-mongo')(express);
// SET UP THE APP
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set("view options", {layout: false}); // from http://stackoverflow.com/questions/13765315/opening-html-file-using-express-js
app.set('port', process.env.PORT || 8080);
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({
store: new MongoStore({
url: "mongodb://jthurst3:" + process.env.MONGODB_PASSWORD + "@ds043338.mongolab.com:43338/heroku_app17128323"
}),
secret:'secretkey'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use("/assets", express.static(__dirname + "/assets")); // technique from https://github.com/sjuvekar/3Dthon/blob/master/web.js
app.use("/models", express.static(__dirname + "/models"));
// Initialize mongoose DB
mongooseDB.mongooseInit();
// Passport js sessions
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// Facebook login and callbacks
app.get('/auth/facebook', function(request, response, next) {
request.session.returnURL = request.headers.referer;
return facebookAuth.facebookAuth(request, response, next);});
app.get('/auth/facebook/callback', function(request, response, next) {
return facebookAuth.facebookAuthWithCallback(request, response, next)});
// Twitter login and callbacks
app.get('/auth/twitter', function(request, response, next) {
request.session.returnURL = request.headers.referer;
return twitterAuth.twitterAuth(request, response, next);
})
app.get('/auth/twitter/callback', function(request, response, next) {
return twitterAuth.twitterAuthWithCallback(request, response, next)});
// Google login and callbacks
app.get('/auth/google', function(request, response, next) {
request.session.returnURL = request.headers.referer;
return googleAuth.googleAuth(request, response, next);});
app.get('/auth/google/callback', function(request, response, next) {
return googleAuth.googleAuthWithCallback(request, response, next)});
var render2 = function(destination, options, request, response) {
options.alert = request.session.alert;
options.alertType = request.session.alertType;
options.alertText = request.session.alertText;
options.user = request.user;
async.series([
function(callback) {
Field.find({}, function(err, fieldList) {
options.fields = fieldList;
response.render(destination, options);
});
callback();
}
], function(err) {
if(err) return next(err);
// reset some session variables
request.session.alert = false;
request.session.alertType = null;
request.session.alertText = null;
})
}
// Render homepage (note trailing slash): example.com/
app.get('/', function(request, response) {
CarouselItem.find({}, function(err, items) {
if(err) {
console.log(err);
response.redirect('/servererror');
} else {
render2("index", {
navid: 1,
carouselItems: items
}, request, response);
}
});
});
app.get('/about', function(request, response) {
render2("about", {navid:2}, request, response);
});
app.get('/contact', function(request, response) {
render2("contact", {navid:3}, request, response);
});
app.get('/problem/:probName', function(request, response) {
var problem = Problem.findOne({nid:request.params.probName}, function(err, result) {
if(!err) {
if(result == null) {
render2("notFound", {}, request, response);
} else {
// find an easy way to sort a MongoDB subarray so we don't have to keep "find"ing the same problem
render2("problem/problem", {problem: result}, request, response);
}
}
else {
console.log(err);
}
});
});
app.get('/categories/:field', function(request, response) {
var category = Field.findOne({nid:request.params.field}, function(err, result) {
if(!err) {
if(result == null) {
render2("notFound", {}, request, response);
} else {
var problems = Problem.find({topic: result.name}, function(error, result2) {
render2("categories/field", {field: result, problems: result2}, request, response);
});
}
} else {
console.log(err);
}
});
});
app.get('/newProblem', function(request, response) {
var q = request.query;
var ses = request.session;
if(!request.user) {
console.log("User not logged in.");
ses.alert = true;
ses.alertType = "alert-error";
ses.alertText = "You must be logged in to create an unsolved problem page.";
response.redirect(request.headers.referer || "/");
} else {
// check to see if problem already exists
Problem.findOne({nid: q.problemAbbrev}, function(err, problems) {
if(err) {
console.log(err);
response.redirect("/servererror");
} else if(problems == null || problems == []) {
// problem does not exist, create new problem
// modified from http://www.sebastianseilund.com/nodejs-async-in-practice
async.series([
function(callback) {
NewProblem.newProblem(q.problemAbbrev, q.problemName, q.topic, q.topicid, q.subtopic);
callback();
}
], function(err) {
if(err) return next(err);
response.redirect('/problem/' + q.problemAbbrev);
});
} else {
// problem exists, don't submit
ses.alert = true;
ses.alertType = "alert-error";
ses.alertText = "Problem \"" + q.topicid + "\" already exists.";
response.redirect("/categories/" + q.topicid); // change later to include alert
}
});
}
})
app.get('/submitEdit', function(request, response) {
var q = request.query;
var ses = request.session;
if(!request.user) {
ses.alert = true;
ses.alertType = "alert-error";
ses.alertText = "You must be logged in to edit site content.";
response.redirect('/problem/' + q.problemName);
}
else {
async.series([
function(callback) {
SingleEdit.saveEdit(request.user._id, q.problemId, q.problemOldSection, q.problemSection, q.sectionName, q.problemOldText, q.problemNewText);
callback();
}], function(err) {
if(err) return next(err);
response.redirect('/problem/' + q.problemId);
});
}
});
app.get('/newSection', function(request, response) {
var q = request.query;
var ses = request.session;
if(!request.user) {
ses.alert = true;
ses.alertType = "alert-error";
ses.alertText = "You must be logged in to edit site content.";
response.redirect('/problem/' + q.problemName);
}
else {
async.series([
function(callback) {
NewSection.newSection(q.name, q.section, q.sectionName, parseInt(q.sectionNumber));
callback();
}], function(err) {
if(err) return next(err);
response.redirect('/problem/' + q.name);
});
}
});
app.get('/newField', function(request, response) {
var q = request.query;
var ses = request.session;
if(!request.user) {
ses.alert = true;
ses.alertType = "alert-error";
ses.alertText = "You must be logged in to create a new field.";
response.redirect('/categories/' + q.currentField);
}
else {
async.series([
function(callback) {
NewField.newField(q.name, q.nid);
callback();
}], function(err) {
if(err) return next(err);
response.redirect('/categories/' + q.nid);
});
}
});
app.get('/sandboxEdit', function(request, response) {
var q = request.query;
var ses = request.session;
if(!request.user) {
ses.alert = true;
ses.alertType = "alert-error";
ses.alertText = "You must be logged in to edit your sandbox.";
response.redirect(request.headers.referer || "/");
} else {
// update user sandbox
User.update({_id: request.user}, {$set: {"sandbox":q.sandboxNewText}}, function(err, result) {
if(err) {
console.log(err);
response.redirect('/servererror');
}
else {
response.redirect('/dashboard');
}
});
}
});
app.get('/newCarouselItem', function(request, response) {
var q = request.query;
var ses = request.session;
if(!request.user) {
console.log("not valid superuser (submitting a carousel item).");
response.redirect("/notFound");
} else if(ses.passport.user != process.env.PASSPORT_SUPERUSER) {
console.log("not valid superuser (submitting a carousel item).");
response.redirect("/notFound");
} else {
async.series([
function(callback) {
NewCarouselItem.newCarouselItem(q.number, q.pic, q.url, q.color, q.text, q.subtext);
callback();
}], function(err) {
if(err) return next(err);
response.redirect(request.headers.referer || "/");
});
}
});
app.get('/newFAQ', function(request, response) {
var q = request.query;
var ses = request.session;
if(!request.user) {
console.log("not valid superuser (submitting an FAQ question).");
response.redirect("/notFound");
} else if(ses.passport.user != process.env.PASSPORT_SUPERUSER) {
console.log("not valid superuser (submitting an FAQ question).");
response.redirect("/notFound");
} else {
async.series([
function(callback) {
NewFAQ.newFAQ(q.number, q.question, q.answer);
callback();
}], function(err) {
if(err) return next(err);
response.redirect('/faq');
});
}
});
app.get('/faq', function(request, response) {
FAQ.find({}, function(err, list) {
if(err) {
console.log(err);
response.redirect("/servererror");
} else {
render2("faq", {navid:4, questions: list}, request, response);
}
});
});
app.get('/logout', function(request, response) {
request.logout();
var ses = request.session;
ses.alert = true;
ses.alertType = "alert-success";
ses.alertText = "Successfully logged out.";
response.redirect('/');
});
app.get('/dashboard', function(request, response) {
var ses = request.session;
if(!request.user) {
ses.alert = true;
ses.alertType = "alert-error";
ses.alertText = "You are not logged in.";
response.redirect(request.headers.referer || "/");
}
else {
Edit.find({user: request.user._id}, function(err,list) {
if(err) {
console.log(err);
response.redirect("/servererror");
}
else {
render2("dashboard", {
navid:5,
edits: list
}, request, response);
}
});
}
});
app.get('/superuser', function(request, response) {
if(!request.user) {
console.log("not valid superuser.");
response.redirect("/notFound");
} else if(request.session.passport.user != process.env.PASSPORT_SUPERUSER) {
console.log("not valid superuser.");
response.redirect("/notFound");
} else {
console.log("valid superuser");
FAQ.find({}, function(err, questions) {
CarouselItem.find({}, function(err2, items) {
render2("superuser", {questions: questions, carouselItems: items}, request, response);
});
});
}
});
app.get('/sortProblems', function(request, response) {
if(!request.user) {
console.log("not valid superuser (sort problem sections).");
response.redirect("/notFound");
} else if(request.session.passport.user != process.env.PASSPORT_SUPERUSER) {
console.log("not valid superuser (sort problem sections).");
response.redirect("/notFound");
} else {
console.log("valid superuser");
Problem.update({},
{$push: {"content": {
$each: [],
$sort: {"sectionNumber": 1},
$slice: Infinity
}}}, function(err, result) {
if(err) {
console.log(err);
response.redirect('/servererror');
} else {
response.redirect(request.headers.referer || '/problem/collatz');
}
});
}
});
app.get('/acknowledgements', function(request, response) {
render2("acknowledgements", {}, request, response);
});
app.get('/servererror', function(request, response) {
render2("servererror", {}, request, response);
})
app.get('*', function(request, response) {
render2('notFound', {user: request.user}, request, response);
});
// start the server
http.createServer(app).listen(app.get('port'), function() {
console.log("Listening on " + app.get('port'));
});