-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·149 lines (132 loc) · 4.33 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
/**
* Module dependencies.
*/
var express = require('express');
var cookieParser = require('cookie-parser');
var compress = require('compression');
var session = require('express-session');
var bodyParser = require('body-parser');
var logger = require('morgan');
var errorHandler = require('errorhandler');
var methodOverride = require('method-override');
var MongoStore = require('connect-mongo')({ session: session });
var flash = require('express-flash');
var path = require('path');
var mongoose = require('mongoose');
var passport = require('passport');
var expressValidator = require('express-validator');
var swig = require('swig');
/**
* Controllers (route handlers).
*/
var loginController = require('./controllers/login');
var routesController = require('./controllers/routes');
/**
* API keys and Passport configuration.
*/
var config = require('./config/config');
/**
* Create Express server.
*/
var app = express();
/**
* Connect to MongoDB.
*/
mongoose.connect(process.env.MONGOLAB_URI);
mongoose.connection.on('error', function () {
console.error('MongoDB Connection Error. Make sure MongoDB is running.');
});
/**
* Swig Templating Engine
*/
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view cache', false);
swig.setDefaults({ cache: false });
/**
* Express configuration.
*/
app.set('port', process.env.PORT || 8000);
app.use(compress());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(expressValidator());
app.use(methodOverride());
app.use(cookieParser());
app.use(session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
store: new MongoStore({
url: process.env.MONGOLAB_URI,
auto_reconnect: true
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
var hour = 3600000;
var day = hour * 24;
var week = day * 7;
app.use(express.static(path.join(__dirname, 'public'), { maxAge: week }));
var loginUrl = 'https://www.topcoder.com/?action=showlogin&next=http://tcojudging.topcoder.com';
app.use(function (req, res, next) {
if (req.isAuthenticated()){
if(!req.user.eligibleForJudging){
return res.render('error', { message: 'You are not eligible for judging.' });
}
return next();
}
if (typeof req.cookies.tcjwt != 'undefined') {
req.body.email = req.cookies.tcjwt;
req.body.password = ' ';
passport.authenticate('local', function (err, user, info) {
if (err) return next(err);
req.logIn(user, function (err) {
if (err) return next(err);
next();
});
})(req, res, next);
} else {
res.redirect(loginUrl);
/** TODO: To be removed */
// var cookie = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3RvcGNvZGVyLmF1dGgwLmNvbS8iLCJzdWIiOiJnb29nbGUtb2F1dGgyfDExMTg1ODczMDE2NDUzMzY5OTI4NCIsImF1ZCI6IjZad1pFVW8yWks0YzUwYUxQcGd1cGVnNXYyRmZ4cDlQIiwiZXhwIjoxNzkwODU4MjQ2LCJpYXQiOjE0MzA4NTgyNDZ9.A3HeisoV92z70x1nXGQ7YhP_0qCuX3dSTA4jbhtF8wA';
// res.cookie('tcjwt', cookie, { maxAge: week, httpOnly: true });
// req.body.email = 'jdouglas@appirio.com';
// req.body.password = ' ';
// passport.authenticate('local', function (err, user, info) {
// if (err) return next(err);
// req.logIn(user, function (err) {
// if (err) return next(err);
// next();
// });
// })(req, res, next);
// end
}
});
/**
* Main routes.
*/
app.get('/', routesController.index);
app.get('/logout', routesController.logout);
app.post('/create-submission', routesController.createSubmission);
app.get('/get-submission', routesController.getSubmission);
app.post('/submit-review', routesController.submitReview);
app.get('/get-submissions', routesController.getSubmissions);
app.get('/calculate-scores', routesController.calculateScores);
app.get('/submission-details/:id', routesController.getSubmissionDetails);
/**
* 500 Error Handler.
*/
app.use(errorHandler());
/**
* Start Express server.
*/
app.listen(app.get('port'), function () {
console.log('Judging Roulette [%s] server is listening on port %d', app.get('env'), app.get('port'));
});
module.exports = app;