-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
63 lines (55 loc) · 1.73 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
import express from 'express'
import mongoose from 'mongoose'
import asyncExpress from 'async-express'
const app = express()
// Load models into memory
mongoose.set('useCreateIndex', true)
require('./models/entry')
require('./models/event')
require('./models/race')
require('./models/rider')
require('./models/promoter')
require('./models/bib')
require('./models/series')
require('./models/seriesPromoter')
require('./models/passing')
// Setup global express middleware (json parsing and cors)
app.use(express.json())
app.use((_, res, next) => {
res.set('Access-Control-Allow-Origin', '*')
res.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PUT')
res.set('Access-Control-Allow-Headers', 'content-type')
next()
})
if (process.env.NODE_ENV === 'test') {
process.env.WEB_TOKEN_SECRET = 'secret'
process.env.DB_URI = 'mongodb://127.0.0.1:27017/test'
}
const mongoConnect = asyncExpress(async (_1, _2, next: any) => {
await mongoose.connect(process.env.DB_URI, {
connectTimeoutMS: 5000,
useNewUrlParser: true,
})
next()
})
const mongoDisconnect = asyncExpress(async (_1, _2, next) => {
await mongoose.disconnect()
next()
})
/**
* Establish a connection to the mongo database, then continue the request
**/
app.use(mongoConnect)
// Load the routes on the express router
require('./routes/event').default(app)
require('./routes/promoter').default(app)
require('./routes/race').default(app)
require('./routes/rider').default(app)
require('./routes/series').default(app)
require('./routes/bib').default(app)
require('./routes/passing').default(app)
require('./routes/leaderboard').default(app)
if (process.env.NODE_ENV !== 'test' && process.env.NODE_ENV !== 'development') {
app.use(mongoDisconnect)
}
export default app