generated from BritishYouthBandAssociation/TemplateSite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (122 loc) · 3.63 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
'use strict';
// Import modules
const path = require('path');
const session = require('express-session');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
// Initialise library path
const libPath = path.join(__dirname, process.env.LIB_PATH ?? '../Library');
global.__lib = libPath;
// Import library functions
const {
helpers: {
ConfigHelper,
},
Server,
constants
} = require(libPath);
//set global base dir
global.__approot = __dirname;
async function main() {
// Initialise express app
const app = Server({
dir: __dirname
});
// Import configuration
const serverOptions = ConfigHelper.importJSON(path.join(__dirname, 'config'), 'server');
// Add logging middleware
if (serverOptions.logging) {
app.enableLogging('tiny', (req) => /(^\/(js|css|fonts|assets|favicon)|(png|jpg|css))/.test(req.path));
}
// Set up handlebars templating engine
app.useHandlebars('hbs', {
extname: '.hbs',
runtimeOptions: {
allowProtoPropertiesByDefault: true,
allowProtoMethodsByDefault: true
}
});
// Serve the production vue script if in production environment
app.get('/js/vue.js', (req, res, next) => {
if (process.env.NODE_ENV === 'production') {
return res.redirect('/js/vue.min.js');
}
next();
});
app.addStaticDir(path.join(__dirname, 'public'));
// Get database models and connection
const dbPath = path.join(libPath, 'models');
const db = await require(dbPath)(path.join(__dirname, 'config/db'));
app.registerGlobals({db});
//... and use it for our session stuff too!
const sessionStore = new SequelizeStore({ db: db.sequelize });
await sessionStore.sync();
const sessionConfig = ConfigHelper.importJSON(path.join(__dirname, 'config'), 'session');
sessionConfig.store = sessionStore;
app.use(session(sessionConfig));
//prevent unauthorised access
app.use(async (req, res, next) => {
//allow css/css.map/js files
if (/\.(css|js|css\.map)$/.test(req.path)) {
return next();
}
if (!req.session.user) {
if (!serverOptions.noAuthRequired.includes(req.path)) {
return res.redirect(`/?next=${req.path}`);
}
} else {
req.session.user = await req.db.User.findByPk(req.session.user.id);
req.session.user.bands = await req.db.Organisation.findAll({
include: [{
model: req.db.OrganisationUser,
where: {
UserId: req.session.user.id
},
attributes: []
}],
where: {
OrganisationTypeId: constants.ORGANISATION_TYPE.BAND
}
});
if (!req.session.user.bands.some(b => b.id === req.session.band?.id)) {
req.session.band = null;
}
if (!req.session.band) {
if (req.session.user.bands.length > 0) {
req.session.band = req.session.user.bands[0];
}
}
const resetPath = `/password-reset/${req.session.user.id}`;
const resetAllowedPaths = /^\/(logout$|user\/.*\/password)/;
if (req.session.user.ForcePasswordReset && !req.path.startsWith(resetPath) && !resetAllowedPaths.test(req.path)){
res.redirect(resetPath);
return;
}
}
next();
});
//init locals (for Handlebars)
app.use((req, res, next) => {
res.locals.page = req.path.toLowerCase();
res.locals.session = req.session;
res.locals.uploadServer = serverOptions.uploadServer;
next();
});
app.loadRoutes();
/*
* If the request gets to the bottom of the route stack, it doesn't
* have a defined route and therefore a HTTP status code 404 is sent
* and an error page shown
*/
app.use((_, res) => {
res.status(404).render('error', {
title: 'Error',
code: 404,
msg: 'Page Not Found'
});
});
// Start the server
app.listen(serverOptions.port, () => {
console.log(`Server listening on :${serverOptions.port}`);
});
}
main();