-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
208 lines (173 loc) · 7.42 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
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var sessionParser = require('./session');
var cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
var logger = require('./logger')
const common = require("./common_functions");
const passport = require('passport');
const LocalStrategy = require("passport-local").Strategy;
const rateLimit = require("express-rate-limit");
const { RecaptchaEnterpriseServiceClient } = require('@google-cloud/recaptcha-enterprise');
// Get settings
module.exports = common.getJsonFile("/settings").then(function (appSettings) {
var app = express();
app.set('appSettings', appSettings);
appSettings.database.useNullAsDefault = true
const knex = require('knex')(appSettings.database);
// Set knex instance to app
app.set('knex', knex);
// Init logger
var log = logger.app(path.parse(__filename).name);
app.locals.env = "production"
// Set rate limiter
const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
// Create assesment client
const client = new RecaptchaEnterpriseServiceClient();
const projectPath = client.projectPath(appSettings.recaptcha.projectId);
app.set('recaptcha', {
client: client,
projectPath: projectPath
});
// Check if users table is present
knex("users")
.where({ username: "admin" })
.first()
.then((row) => {
if (typeof (row) == "undefined") {
log.error("Database not initialized. Run npm install to initialize database automatically");
}
})
.catch((error) => {
log.error("Database not initialized. Run npm install to initialize database automatically");
})
// Update current count
common.updateCurrentOrder(knex);
// Load Quiz questions templates
common.getJsonFile("/question_templates")
.then(function (questionTemplates) {
// Add default "message" question template.
questionTemplates.unshift({
"id": "message",
"name": "Message",
"parameters": [
{
"type": "message",
"name": "Bericht",
"id": "default"
}
]
})
app.set('questionTemplates', questionTemplates);
})
.catch((err) => { log.error(`app: cannot load questions template file (${err})`) })
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// Session handler
app.use(sessionParser.handler);
// Passport authentication
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(
function (username, password, done) {
knex("users")
.where({ username: username }).first()
.then(user => {
var hash = common.hashPassword(password, user.salt);
// Found user, check password
if (hash != user.password) {
return done(null, false, { message: 'Incorrect password.' });
}
log.info(`User ${username} logged in`);
return done(null, user);
})
.catch(error => {
return done(null, false, { message: `Incorrect username.` });
})
}
));
passport.serializeUser(function (user, done) {
return done(null, user.id);
});
passport.deserializeUser(function (id, done) {
knex("users")
.where({ id: id }).first()
.then(user => {
return done(null, user);
})
.catch(error => {
return done(null, false);
})
});
// Web logger instance
app.use(logger.express);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var authRouter = require('./routes/auth_routes');
var userRouter = require('./routes/user_routes');
var indexRouter = require('./routes/index_routes');
var quizRouter = require('./routes/quiz_routes');
var registrationRouter = require('./routes/registration_routes');
var controlRouter = require('./routes/control_routes');
var scoreRouter = require('./routes/score_routes');
var teamsRouter = require('./routes/teams_routes');
var questionsRouter = require('./routes/questions_routes');
// Make specific modules public
app.use('/scripts/jquery', express.static(__dirname + '/node_modules/jquery/dist/'));
app.use('/scripts/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/'));
app.use('/scripts/reconnecting-websocket', express.static(__dirname + '/node_modules/reconnecting-websocket/dist/'));
app.use('/scripts/moment', express.static(__dirname + '/node_modules/moment/min'));
app.use('/scripts/icons', express.static(__dirname + '/node_modules/@mdi/font'));
app.use('/scripts/popper', express.static(__dirname + '/node_modules/popper.js/dist/umd'));
app.use('/scripts/handlebars', express.static(__dirname + '/node_modules/handlebars/dist'));
app.use('/scripts/sortable', express.static(__dirname + '/node_modules/sortablejs'));
app.use('/scripts/jquery-sortable', express.static(__dirname + '/node_modules/jquery-sortablejs/'));
app.use('/scripts/tsparticles', express.static(__dirname + '/node_modules/tsparticles/dist/'));
app.use('/scripts/jquery-particles', express.static(__dirname + '/node_modules/jquery-particles/dist/'));
app.use('/scripts/timeago', express.static(__dirname + '/node_modules/timeago.js/dist/'));
app.use('/login', limiter, authRouter);
app.use('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
// Main route defenitions
app.use('/quiz', limiter, quizRouter);
app.use('/registration', limiter, registrationRouter);
app.use('/registreren', limiter, registrationRouter);
app.use('/', indexRouter);
app.use('/user', common.isAuthed, userRouter);
app.use('/control', common.isAuthed, controlRouter);
app.use('/score', common.isAuthed, scoreRouter);
app.use('/teams', common.isAuthed, teamsRouter);
app.use('/questions', common.isAuthed, questionsRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
return app;
})
.catch((err) => {
if (err.message.includes("ENOENT") && err.message.includes("settings")) {
log.error(`Cannot load settings file: settings.json. You have to rename settings_empty.json to settings.json and alter the settings in de file.`)
} else {
console.log(err.stack)
}
})