forked from TechnoX/rcj-rescue-scoring
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
executable file
·309 lines (259 loc) · 14.2 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
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
// -*- tab-width: 2 -*-
//========================================================================
// Libraries
//========================================================================
var env = require('node-env-file')
env('process.env')
var express = require('express');
var RateLimit = require('express-rate-limit');
const { createBullBoard } = require('@bull-board/api')
const { BullAdapter } = require('@bull-board/api/bullAdapter')
const { ExpressAdapter } = require('@bull-board/express')
const compression = require('compression')
var path = require('path')
var favicon = require('serve-favicon')
var logger = require('./config/logger').mainLogger
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
const mongo_express = require('mongo-express/lib/middleware')
const mongo_express_config = require('./mongo_express_config')
// db
var db = require('./config/db')
var mongoose = require('mongoose')
// auth
var pass = require('./config/pass')
var passport = require('passport')
// session
var session = require('express-session')
var MongoStore = require('connect-mongo')
var bodyParser = require('body-parser');
const limiter = new RateLimit({
windowMs: process.env.LIMITER_WINDOW_MS || 1000, // 1 sec
max: process.env.LIMITER_MAX || 200
});
const bullAdapter = new ExpressAdapter();
const {mailQueue} = require("./queue/mailQueue")
const {backupQueue} = require("./queue/backupQueue")
const { addQueue, removeQueue, setQueues, replaceQueues } = createBullBoard({
queues: [
new BullAdapter(mailQueue),
new BullAdapter(backupQueue)
],
serverAdapter:bullAdapter
})
var app = express();
app.set('trust proxy', true);
async function bootstrap(){
//========================================================================
// Routes require
//========================================================================
//========================================================================
// Static routes
//========================================================================
var homeRoute = require('./routes/home')
var lineRoute = require('./routes/line')
var mazeRoute = require('./routes/maze')
var simulationRoute = require('./routes/simulation')
var loginRoute = require('./routes/login')
var adminRoute = require('./routes/admin')
var localesRoute = require('./routes/locales')
var signageRoute = require('./routes/signage')
var shortRoute = require('./routes/shortURL')
var serviceRoute = require('./routes/service')
var documentRoute = require('./routes/document')
var registrationRoute = require('./routes/registration')
var myPageRoute = require('./routes/mypage')
//========================================================================
// Api routes
//========================================================================
var apiAuthRoute = require('./routes/api/auth')
var apiLineMapsRoute = require('./routes/api/lineMaps')
var apiMazeMapsRoute = require('./routes/api/mazeMaps')
var apiTeamsRoute = require('./routes/api/teams')
var apiRoundsRoute = require('./routes/api/rounds')
var apiFieldsRoute = require('./routes/api/fields')
var apiLineRunsRoute = require('./routes/api/lineRuns')
var apiMazeRunsRoute = require('./routes/api/mazeRuns')
var apiSimulationRunsRoute = require('./routes/api/simRuns')
var apiCompetitionsRoute = require('./routes/api/competitions')
var apiUserRoute = require('./routes/api/users')
var apiSignageRoute = require('./routes/api/signage')
var apiBackupRoute = require('./routes/api/backup')
var apiShortURL = require('./routes/api/shortURL')
var apiDocumentRoute = require('./routes/api/document')
var apiMailRoute = require('./routes/api/mail')
var apiRegistrationRoute = require('./routes/api/registration')
var apiReservationRoute = require('./routes/api/reservation')
var apiCabinetRoute = require('./routes/api/cabinet')
var apiSurveyRoute = require('./routes/api/survey')
var apiRankingRoute = require('./routes/api/ranking')
var apiTechnicalChallengeRoute = require('./routes/api/technicalChallenge')
//========================================================================
// Configuration
//========================================================================
app.use(compression())
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
/** Setting up the correct view engine - we are using pug */
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
/*
* Log
*/
//app.use(morgan('short'));
/*
* Config
*/
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public/images', 'favicon.ico')))
//app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
/*
* Session and passport for auth
*/
// init passport and session
app.use(session({
store: MongoStore.create({ mongoUrl: process.env.DB_CONNECT_STR, mongoOptions:{useUnifiedTopology: true}}),
secret: process.env.SESSION_SECRET || 'rcjscoring',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 10 * 60 * 60 * 1000
}
}))
app.use(passport.initialize())
app.use(passport.session())
// apply rate limiter to all requests
app.use(limiter);
//========================================================================
// Route configuration
//========================================================================
//========================================================================
// API Calls
//========================================================================
app.use('/api/auth', apiAuthRoute)
app.use('/api/maps/line', [apiLineMapsRoute.public, pass.ensureLoginApi, apiLineMapsRoute.private, pass.ensureAdminApi, apiLineMapsRoute.admin])
app.use('/api/maps/maze', [apiMazeMapsRoute.public, pass.ensureLoginApi, apiMazeMapsRoute.private, pass.ensureAdminApi, apiMazeMapsRoute.admin])
app.use('/api/teams', [apiTeamsRoute.public, pass.ensureLoginApi, apiTeamsRoute.private, pass.ensureAdminApi, apiTeamsRoute.admin])
app.use('/api/rounds', [apiRoundsRoute.public, pass.ensureLoginApi, apiRoundsRoute.private, pass.ensureAdminApi, apiRoundsRoute.admin])
app.use('/api/fields', [apiFieldsRoute.public, pass.ensureLoginApi, apiFieldsRoute.private, pass.ensureAdminApi, apiFieldsRoute.admin])
app.use('/api/runs/line', [apiLineRunsRoute.public, pass.ensureLoginApi, apiLineRunsRoute.private, pass.ensureAdminApi, apiLineRunsRoute.admin])
app.use('/api/runs/maze', [apiMazeRunsRoute.public, pass.ensureLoginApi, apiMazeRunsRoute.private, pass.ensureAdminApi, apiMazeRunsRoute.admin])
app.use('/api/runs/simulation', [apiSimulationRunsRoute.public, pass.ensureLoginApi, apiSimulationRunsRoute.private, pass.ensureAdminApi, apiSimulationRunsRoute.admin])
app.use('/api/competitions', [apiCompetitionsRoute.public, pass.ensureLoginApi, apiCompetitionsRoute.private, pass.ensureAdminApi, apiCompetitionsRoute.admin])
app.use('/api/users', [pass.ensureLoginApi, apiUserRoute.admin, pass.ensureSuperApi , apiUserRoute.super])
app.use('/api/signage', [pass.ensureLoginApi, apiSignageRoute.private, pass.ensureAdminApi, apiSignageRoute.admin])
app.use('/api/backup', [pass.ensureAdminApi, apiBackupRoute.admin])
app.use('/api/short', [pass.ensureSuperApi , apiShortURL.super])
app.use('/api/document', [apiDocumentRoute.public, pass.ensureLoginApi, apiDocumentRoute.private, pass.ensureAdminApi, apiDocumentRoute.admin])
app.use('/api/mail', [apiMailRoute.public, pass.ensureLoginApi, apiMailRoute.private, pass.ensureAdminApi, apiMailRoute.admin])
app.use('/api/registration', [apiRegistrationRoute.public, pass.ensureLoginApi, apiRegistrationRoute.private, pass.ensureAdminApi, apiRegistrationRoute.admin])
app.use('/api/reservation', [apiReservationRoute.public, pass.ensureLoginApi, apiReservationRoute.private, pass.ensureAdminApi, apiReservationRoute.admin])
app.use('/api/cabinet', [apiCabinetRoute.public, pass.ensureLoginApi, apiCabinetRoute.private, pass.ensureAdminApi, apiCabinetRoute.admin])
app.use('/api/survey', [apiSurveyRoute.public, pass.ensureLoginApi, apiSurveyRoute.private, pass.ensureAdminApi, apiSurveyRoute.admin])
app.use('/api/ranking', [apiRankingRoute.public, pass.ensureLoginApi, apiRankingRoute.private, pass.ensureAdminApi, apiRankingRoute.admin])
app.use('/api/technicalChallenge', [pass.ensureLoginApi, apiTechnicalChallengeRoute.private, pass.ensureAdminApi, apiTechnicalChallengeRoute.admin])
app.get('/api/ip', (request, response) => response.send(request.ip))
//========================================================================
// Website static pages(ish)
//========================================================================
app.use('/login', pass.ensureNotAuthenticated, loginRoute)
app.use('/logout', pass.ensureAuthenticated, function (req, res, next) {
req.logout(req.user, err => {
if(err) return next(err);
res.redirect("/home");
});
})
app.use('/home', [homeRoute.public, pass.ensureAuthenticated, homeRoute.private, pass.ensureAdmin, homeRoute.admin])
app.use('/locales', localesRoute)
app.use('/s', shortRoute.public)
app.use('/service', serviceRoute)
app.use('/line', [lineRoute.public, pass.ensureAuthenticated, lineRoute.private, pass.ensureAdmin, lineRoute.admin])
app.use('/maze', [mazeRoute.public, pass.ensureAuthenticated, mazeRoute.private, pass.ensureAdmin, mazeRoute.admin])
app.use('/simulation', [simulationRoute.public, pass.ensureAuthenticated, simulationRoute.private, pass.ensureAdmin, simulationRoute.admin])
app.use('/signage', [pass.ensureAuthenticated, signageRoute.private, pass.ensureAdmin, signageRoute.admin])
app.use('/admin', pass.ensureAdmin, adminRoute)
bullAdapter.setBasePath('/admin/queues')
app.use('/admin/queues', pass.ensureSuper, bullAdapter.getRouter())
app.use('/admin/mongo', pass.ensureSuper, await mongo_express(mongo_express_config))
app.use('/document', [documentRoute.public, pass.ensureAuthenticated, documentRoute.private, pass.ensureAdmin, documentRoute.admin])
app.use('/registration', [registrationRoute.public, pass.ensureAuthenticated, registrationRoute.private, pass.ensureAdmin, registrationRoute.admin])
app.use('/mypage', [myPageRoute.public, pass.ensureAuthenticated, myPageRoute.private, pass.ensureAdmin, myPageRoute.admin])
//========================================================================
// Custom routes
//========================================================================
/*
* This is called last in the routing config, therefor it'll take care of 404s
*/
app.use(function (req, res, next) {
logger.error("404 Not Found: " + req.originalUrl)
var err = new Error('Not Found: ' + req.originalUrl)
err.status = 404
next(err)
})
//========================================================================
// Error handling
//========================================================================
process.on('uncaughtException', function(err) {
console.log("SERVER ERROR");
console.log(err);
});
app.use(function (err, req, res, next) {
// is at base, send to login
if (req.originalUrl === "/") {
res.redirect("home")
}
/*
* Check if it is an api or static page miss (404)
*
* One of the big things to do for scalability is to separate tcp server,static server and application server (api).
*
* Right now they are mixed and running on a single core togeather. To be able to use Node.js to 100% the application
* part of the server should run alone on an own process. And the static files (CSS, HTML and Javascript(frontend)) should
* for performance reasons not run on Node.js but on nginx (http://nginx.org/). One of the fastest static servers out there used
* by many companies. Also it can work as a reverse proxy to support multi Node.js clusters.
*
*/
else {
if (err.status != 404) {
logger.error(err)
}
// since we are running api and static website on same we need to hack the different custom routes
var stringSplit = req.originalUrl.split("/")
//res.status(err.status || 500)
if (stringSplit[1] !== undefined && stringSplit[1] === "api") {
res.status(404).send({
message: "404 Not found"
})
} else {
// in in development show stacktrace
if (app.get('env') === 'development') {
res.render('error', {
message: err.message,
error: err
})
}
// in production :(
else {
res.status(err.status || 500)
res.render('error', {
message: err.message,
error: {}
})
}
}
}
})
}
bootstrap();
module.exports = app