-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
46 lines (40 loc) · 1.29 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
import cors from 'cors'
import express from 'express'
import preferenceHeaders from 'express-prefer'
import url from 'url'
import program from 'commander'
import { NotFoundError } from './lib/error'
import { httpProblemMiddleware } from './lib/express/problemDetails'
import authentication from './lib/express/authentication'
import { logRequest, logRequestError } from './lib/express/logger'
import { log, error } from './lib/log'
import env from './lib/env'
import('./lib/handlers')
program
.action(() => {
Promise.resolve().then(async () => {
const baseUrl = `${env.BASE_URI}`
const hydraMiddleware = await import('./hydra')
const app = express()
app.enable('trust proxy')
app.use(logRequest)
app.use(preferenceHeaders)
app.use(cors({
exposedHeaders: ['link', 'location'],
}))
app.use(authentication)
app.use(await hydraMiddleware.default())
app.use(function (req, res, next) {
next(new NotFoundError())
})
app.use(logRequestError)
app.use(httpProblemMiddleware)
app.listen((env.PORT || new url.URL(baseUrl).port), () => {
log(`listening at port ${env.PORT}`)
})
}).catch(err => {
error('Failed to start: %O', err)
process.exit(1)
})
})
program.parse(process.argv)