-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
69 lines (55 loc) · 1.67 KB
/
index.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
import path from 'path'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import 'express-async-errors'
import express from 'express'
import session from 'express-session'
import passport from 'passport'
import { inE2EMode, inProduction, inStaging, inTest } from '../config'
import { PORT, SESSION_SECRET } from './util/config'
import { redisStore } from './util/redis'
import logger from './util/logger'
import router from './routes'
import setupAuthentication from './util/oidc'
import { connectToDatabase } from './db/connection'
import seed from './db/seeders'
import setupCron from './util/cron'
const app = express()
app.use(
session({
store: redisStore,
resave: false,
saveUninitialized: false,
secret: inE2EMode || inTest ? 'testing' : SESSION_SECRET,
})
)
app.use(passport.initialize())
app.use(passport.session())
app.use(['/api', '/api'], (req, res, next) => router(req, res, next))
app.use(['/api', '/api'], (_, res) => {
res.sendStatus(404)
})
if (inProduction || inStaging || inTest) {
const DIST_PATH = path.resolve(
dirname(fileURLToPath(import.meta.url)),
'../../dist'
)
const INDEX_PATH = path.resolve(DIST_PATH, 'index.html')
app.use(express.static(DIST_PATH))
app.get('*', (_, res) => res.sendFile(INDEX_PATH))
}
if (process.env.NODE_ENV !== 'test') {
app.listen(PORT, async () => {
await connectToDatabase()
// only seed when in dev environment
if (process.env.NODE_ENV === 'development') {
await seed()
}
await setupAuthentication()
if (inProduction || inStaging) {
await setupCron()
}
logger.info(`Server running on port ${PORT}`)
})
}
export default app