46 lines
883 B
JavaScript
46 lines
883 B
JavaScript
|
const Router = require("@koa/router")
|
||
|
const compress = require("koa-compress")
|
||
|
const zlib = require("zlib")
|
||
|
const { routes } = require("./routes")
|
||
|
|
||
|
const router = new Router()
|
||
|
|
||
|
router
|
||
|
.use(
|
||
|
compress({
|
||
|
threshold: 2048,
|
||
|
gzip: {
|
||
|
flush: zlib.Z_SYNC_FLUSH,
|
||
|
},
|
||
|
deflate: {
|
||
|
flush: zlib.Z_SYNC_FLUSH,
|
||
|
},
|
||
|
br: false,
|
||
|
})
|
||
|
)
|
||
|
.use("/health", ctx => (ctx.status = 200))
|
||
|
|
||
|
// error handling middleware
|
||
|
router.use(async (ctx, next) => {
|
||
|
try {
|
||
|
await next()
|
||
|
} catch (err) {
|
||
|
ctx.log.error(err)
|
||
|
ctx.status = err.status || err.statusCode || 500
|
||
|
ctx.body = {
|
||
|
message: err.message,
|
||
|
status: ctx.status,
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
router.get("/health", ctx => (ctx.status = 200))
|
||
|
|
||
|
// authenticated routes
|
||
|
for (let route of routes) {
|
||
|
router.use(route.routes())
|
||
|
router.use(route.allowedMethods())
|
||
|
}
|
||
|
|
||
|
module.exports = router
|