-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
237 lines (187 loc) · 5.69 KB
/
app.ts
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
import path from 'node:path'
import * as dateTimeFns from '@cityssm/expressjs-server-js/dateTimeFns.js'
import * as htmlFns from '@cityssm/expressjs-server-js/htmlFns.js'
import * as stringFns from '@cityssm/expressjs-server-js/stringFns.js'
import compression from 'compression'
import cookieParser from 'cookie-parser'
import csurf from 'csurf'
import debug from 'debug'
import express from 'express'
import rateLimit from 'express-rate-limit'
import session from 'express-session'
import createError from 'http-errors'
import FileStore from 'session-file-store'
import * as databaseInitializer from './helpers/databaseInitializer.js'
import * as configFunctions from './helpers/functions.config.js'
import routerAdmin from './routes/admin.js'
import routerBatches from './routes/batches.js'
import routerDashboard from './routes/dashboard.js'
import routerLicences from './routes/licences.js'
import routerLogin from './routes/login.js'
import routerReports from './routes/reports.js'
import { version } from './version.js'
const debugApp = debug('general-licence-manager:app')
/*
* INITALIZE THE DATABASE
*/
databaseInitializer.initLicencesDB()
/*
* INITIALIZE APP
*/
export const app = express()
if (!configFunctions.getConfigProperty('reverseProxy.disableEtag')) {
app.set('etag', false)
}
// View engine setup
app.set('views', path.join('views'))
app.set('view engine', 'ejs')
if (!configFunctions.getConfigProperty('reverseProxy.disableCompression')) {
app.use(compression())
}
app.use((request, _response, next) => {
debugApp(`${request.method} ${request.url}`)
next()
})
app.use(express.json())
app.use(
express.urlencoded({
extended: false
})
)
app.use(cookieParser())
app.use(csurf({ cookie: true }))
/*
* Rate Limiter
*/
const limiter = rateLimit({
windowMs: 1000,
max: 25 * Math.max(3, configFunctions.getConfigProperty('users.canLogin').length)
})
app.use(limiter)
/*
* STATIC ROUTES
*/
const urlPrefix = configFunctions.getConfigProperty('reverseProxy.urlPrefix')
if (urlPrefix !== '') {
debugApp('urlPrefix = ' + urlPrefix)
}
app.use(urlPrefix, express.static(path.join('public')))
app.use(
urlPrefix + '/lib/fa',
express.static(path.join('node_modules', '@fortawesome', 'fontawesome-free'))
)
app.use(
urlPrefix + '/lib/cityssm-bulma-webapp-js',
express.static(path.join('node_modules', '@cityssm', 'bulma-webapp-js'))
)
app.use(
urlPrefix + '/lib/cityssm-bulma-js',
express.static(path.join('node_modules', '@cityssm', 'bulma-js', 'dist'))
)
/*
* SESSION MANAGEMENT
*/
const sessionCookieName: string =
configFunctions.getConfigProperty('session.cookieName')
const FileStoreSession = FileStore(session)
// Initialize session
app.use(
session({
store: new FileStoreSession({
path: './data/sessions',
logFn: debug('general-licence-manager:session'),
retries: 20
}),
name: sessionCookieName,
secret: configFunctions.getConfigProperty('session.secret'),
resave: true,
saveUninitialized: false,
rolling: true,
cookie: {
maxAge: configFunctions.getConfigProperty('session.maxAgeMillis'),
sameSite: 'strict'
}
})
)
// Clear cookie if no corresponding session
app.use((request, response, next) => {
if (request.cookies[sessionCookieName] && !request.session.user) {
response.clearCookie(sessionCookieName)
}
next()
})
// Redirect logged in users
function sessionChecker(
request: express.Request,
response: express.Response,
next: express.NextFunction
): void {
if (request.session.user && request.cookies[sessionCookieName]) {
next()
return
}
response.redirect(`${urlPrefix}/login?redirect=${request.originalUrl}`)
}
/*
* ROUTES
*/
// Make the user and config objects available to the templates
app.use((request, response, next) => {
response.locals.buildNumber = version
response.locals.user = request.session.user
response.locals.csrfToken = request.csrfToken()
response.locals.configFunctions = configFunctions
response.locals.dateTimeFns = dateTimeFns
response.locals.stringFns = stringFns
response.locals.htmlFns = htmlFns
response.locals.urlPrefix = configFunctions.getConfigProperty(
'reverseProxy.urlPrefix'
)
next()
})
app.get(`${urlPrefix}/`, sessionChecker, (_request, response) => {
response.redirect(`${urlPrefix}/dashboard`)
})
app.use(`${urlPrefix}/dashboard`, sessionChecker, routerDashboard)
app.use(`${urlPrefix}/licences`, sessionChecker, routerLicences)
if (configFunctions.getConfigProperty('settings.includeBatches')) {
app.use(`${urlPrefix}/batches`, sessionChecker, routerBatches)
}
app.use(`${urlPrefix}/reports`, sessionChecker, routerReports)
app.use(`${urlPrefix}/admin`, sessionChecker, routerAdmin)
app.all(`${urlPrefix}/keepAlive`, (_request, response) => {
response.json(true)
})
app.use(`${urlPrefix}/login`, routerLogin)
app.get(`${urlPrefix}/logout`, (request, response) => {
if (request.session.user && request.cookies[sessionCookieName]) {
// eslint-disable-next-line unicorn/no-null
request.session.destroy(() => {
response.clearCookie(sessionCookieName)
response.redirect(`${urlPrefix}/`)
})
} else {
response.redirect(`${urlPrefix}/login`)
}
})
// Catch 404 and forward to error handler
app.use((_request, _response, next) => {
next(createError(404))
})
// Error handler
app.use(
(
error: { status: number; message: string },
request: express.Request,
response: express.Response
) => {
// Set locals, only providing error in development
response.locals.message = error.message
response.locals.error =
request.app.get('env') === 'development' ? error : {}
// Render the error page
response.status(error.status || 500)
response.render('error')
}
)
export default app