generated from jefgodesky/api-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
51 lines (40 loc) · 1.52 KB
/
api.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
import { Application, type Router } from '@oak/oak'
import DB from './DB.ts'
import AccountRouter from './collections/accounts/router.ts'
import AuthRouter from './collections/auth/router.ts'
import TaskRouter from './collections/tasks/router.ts'
import UserRouter from './collections/users/router.ts'
import loadRouteParams from './middlewares/load/route-params.ts'
import enforceJsonApiContentType from './middlewares/jsonapi/content-type.ts'
import enforceJsonApiAccept from './middlewares/jsonapi/accept.ts'
import endpointNotFound from './middlewares/endpoint-not-found.ts'
import handleErrors from './middlewares/handle-errors.ts'
import RootRouter from './collections/base/router.ts'
import Swagger from './middlewares/swagger.ts'
const api = new Application()
const routers: Record<string, Router> = {
accounts: AccountRouter,
auth: AuthRouter.router,
tasks: TaskRouter,
users: UserRouter
}
api.use(Swagger.routes())
api.use(handleErrors(routers))
api.use(loadRouteParams(routers))
api.use(enforceJsonApiContentType)
api.use(enforceJsonApiAccept)
const root = new RootRouter(routers)
api.use(root.router.routes())
for (const router of Object.values(routers)) {
api.use(router.routes())
}
api.use(endpointNotFound(routers))
api.addEventListener('listen', ({ hostname, port, secure }) => {
const protocol = secure ? 'https' : 'http'
const url = `${protocol}://${hostname ?? 'localhost'}:${port}`
console.log(`⚡ Listening on ${url}`)
})
api.addEventListener('close', async () => {
await DB.close()
})
export default api