-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
executable file
·195 lines (165 loc) · 5.77 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
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
var express = require('express')
var app = module.exports = express()
global.config = {}
if ('development' == app.get('env')) {
global.config.redis_secret = 'big secret'
//global.config = require('./lib/config')
global.config.status = 'dev'
global.config.login = process.argv[2]
}
if ('production' == app.get('env')) {
global.config.gh_clientId = process.env.clientId;
global.config.gh_secret = process.env.secret;
global.config.redis_secret = process.env.redis_secret;
global.config.wm_clientId = process.env.wm_clientId;
global.config.wm_secret = process.env.wm_secret;
global.config.mail_user = process.env.mail_user;
global.config.mail_pass = process.env.mail_pass;
global.config.status = 'prod';
}
var MACRO = require('./model/macro.js')
, db = require('./model/db')
, fs = require('fs')
, http = require('http')
, https = require('https')
, mongoose = require('mongoose')
, core = require('./core.js')
, cron = require('cron').CronJob
, favicon = require('serve-favicon')
, bodyParser = require('body-parser')
, exprSession = require('express-session')
, passport = require('passport')
, flash = require('connect-flash')
var Users = mongoose.model('Users')
// Refresh challenge cron job
var job = new cron(MACRO.CRON.CHALLENGE, function(){
core.refresh_challenges();
}, function () {}, true, false
);
// Refresh information about repos
var job = new cron(MACRO.CRON.CHALLENGE, function(){
core.update_repo_info();
}, function () {}, true, false
);
// Configuring Passport
require('./auth')(app, passport);
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(flash())
app.use(bodyParser.json())
app.set('admin', MACRO.SUPERUSER);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(favicon(__dirname + "/public/images/rc-logo.ico"));
app.use(express.static(__dirname + '/public'));
app.use(exprSession({
secret : global.config.redis_secret,
resave : true,
saveUninitialized : true,
cookie: {
secure: false,
maxAge: 1800000 //30 min
}
}));
app.use(passport.initialize());
app.use(passport.session());
// GITHUB LOGIN
app.get('/auth/github',
passport.authenticate('github', {
scope : 'email'
})
);
app.get('/auth/github/callback',
passport.authenticate('github', {
successRedirect : '/login',
failureRedirect : '/login'
})
);
app.get('/logout', function(req, res) {
req.logout()
res.redirect('/')
});
// WIKIMEDIA CONNECTION
app.get('/connect/wikimedia',
passport.authenticate('mediawiki', {
scope : 'email'
})
);
app.get('/auth/gerrit',
passport.authenticate('mediawiki', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/login');
});
// Auto login with dummy user in development if 'login' argument is provided
app.use(function (req, res, next) {
if (req.app.get('env') == 'development' && process.argv[2] == 'login' && !req.user) {
username = process.argv[3] || 'dev_user'
userid = parseInt(username, 36)
core.add_user(userid, username, generate_session)
function generate_session(err, user) {
req.login(user, function(err) {
return next();
});
}
} else {
return next();
}
});
// routes defined here
var other = require('./routes/other.js');
app.get('/', other.index);
app.get('/login', other.login);
app.get('/login/:user', other.login);
app.get('/faq', other.faq);
app.get('/guidelines', other.guidelines);
app.get('/contact', other.contact);
app.post('/contact', other.feedback);
var profile = require('./routes/profile.js');
app.get('/:user/notifications', profile.notifications)
var challenge = require('./routes/challenge.js');
app.get('/challenges', challenge.index);
app.get('/challenges/:ch', challenge.one);
app.get('/challenges/:ch/admin', challenge.one);
app.post('/challenges/:ch/edit', challenge.edit);
app.post('/challenges/:ch/rate', challenge.rate);
app.post('/challenges/:ch/admin_add', ensureAuth, challenge.admin_add);
app.post('/challenges/:ch/email_users', ensureAuth, challenge.email_users);
app.post('/challenges/:ch/update_formulae', ensureAuth, challenge.update_formulae);
app.get('/challenges/:ch/admin_remove', ensureAuth, challenge.admin_remove);
app.get('/challenges/:ch/repo_remove', ensureAuth, challenge.repo_remove);
app.get('/challenges/:ch/users', challenge.one);
app.get('/challenges/:ch/pulls', challenge.one);
app.get('/challenges/:ch/join', ensureAuth, challenge.join);
app.get('/challenges/:ch/refresh', ensureAuth, challenge.refresh);
app.get('/challenges/:ch/hide_commit', ensureAuth, challenge.hide_commit);
app.get('/challenges/:ch/display_commit', ensureAuth, challenge.display_commit);
app.get('/challenges/:ch/results', challenge.one);
app.get('/challenges/:ch/update_results', ensureAuth, challenge.update_results);
app.post('/challenges/:ch/blacklist_user', ensureAuth, challenge.blacklist_user);
app.get('/challenges/:ch/unblacklist_user', ensureAuth, challenge.unblacklist_user);
var admin = require('./routes/admin.js');
app.get('/admin', ensureSuper, admin.index);
app.post('/admin/challenge_add', ensureSuper, admin.challenge_add);
/*
This handles all other URLs.
It's main porpose is to serve /user pages and all subpages
but also send 404 response if user does not exist.
*/
app.use(profile.index);
// Make sure user is authenticated middleware
function ensureAuth(req, res, next) {
if (req.user) return next();
res.redirect('/login');
}
// Make sure user is authenticated and root middleware
function ensureSuper(req, res, next) {
if (req.user && MACRO.SUPERUSER.indexOf(req.user.user_name) > -1)
return next();
return res.render('404', {title: "404: File not found"});
}
// Launch server
app.listen(process.env.PORT || 3000, function() {
console.log('Server listening on port 3000.');
});