2020-02-03 10:24:25 +01:00
|
|
|
const Router = require("@koa/router")
|
2021-09-09 14:27:18 +02:00
|
|
|
const {
|
|
|
|
buildAuthMiddleware,
|
|
|
|
auditLog,
|
|
|
|
buildTenancyMiddleware,
|
2022-01-12 12:32:14 +01:00
|
|
|
} = require("@budibase/backend-core/auth")
|
2021-04-12 19:31:58 +02:00
|
|
|
const currentApp = require("../middleware/currentapp")
|
2020-05-07 11:53:34 +02:00
|
|
|
const compress = require("koa-compress")
|
|
|
|
const zlib = require("zlib")
|
2022-02-16 19:23:38 +01:00
|
|
|
const { mainRoutes, staticRoutes, publicRoutes } = require("./routes")
|
2021-01-27 14:55:46 +01:00
|
|
|
const pkg = require("../../package.json")
|
2021-05-05 18:49:34 +02:00
|
|
|
const env = require("../environment")
|
2021-03-26 15:56:34 +01:00
|
|
|
|
2020-05-07 15:04:32 +02:00
|
|
|
const router = new Router()
|
2021-04-21 17:42:44 +02:00
|
|
|
|
2020-05-07 15:04:32 +02:00
|
|
|
router
|
|
|
|
.use(
|
|
|
|
compress({
|
|
|
|
threshold: 2048,
|
|
|
|
gzip: {
|
2021-03-29 16:06:00 +02:00
|
|
|
flush: zlib.constants.Z_SYNC_FLUSH,
|
2020-05-07 15:04:32 +02:00
|
|
|
},
|
|
|
|
deflate: {
|
2021-03-29 16:06:00 +02:00
|
|
|
flush: zlib.constants.Z_SYNC_FLUSH,
|
2020-05-07 15:04:32 +02:00
|
|
|
},
|
2020-05-18 15:58:39 +02:00
|
|
|
br: false,
|
2020-05-06 21:29:47 +02:00
|
|
|
})
|
2020-05-07 15:04:32 +02:00
|
|
|
)
|
|
|
|
.use(async (ctx, next) => {
|
|
|
|
ctx.config = {
|
2020-05-14 16:12:30 +02:00
|
|
|
jwtSecret: env.JWT_SECRET,
|
2020-06-03 13:29:42 +02:00
|
|
|
useAppRootPath: true,
|
2020-04-08 17:57:27 +02:00
|
|
|
}
|
2020-05-07 15:04:32 +02:00
|
|
|
await next()
|
2020-05-07 11:53:34 +02:00
|
|
|
})
|
2021-05-04 12:32:22 +02:00
|
|
|
.use("/health", ctx => (ctx.status = 200))
|
|
|
|
.use("/version", ctx => (ctx.body = pkg.version))
|
2021-09-06 16:48:46 +02:00
|
|
|
// re-direct before any middlewares occur
|
|
|
|
.redirect("/", "/builder")
|
2021-04-28 19:13:21 +02:00
|
|
|
.use(
|
|
|
|
buildAuthMiddleware(null, {
|
|
|
|
publicAllowed: true,
|
|
|
|
})
|
|
|
|
)
|
2021-08-05 10:59:08 +02:00
|
|
|
// nothing in the server should allow query string tenants
|
2021-09-06 17:01:45 +02:00
|
|
|
// the server can be public anywhere, so nowhere should throw errors
|
|
|
|
// if the tenancy has not been set, it'll have to be discovered at application layer
|
|
|
|
.use(
|
|
|
|
buildTenancyMiddleware(null, null, {
|
|
|
|
noTenancyRequired: true,
|
|
|
|
})
|
|
|
|
)
|
2021-04-12 19:31:58 +02:00
|
|
|
.use(currentApp)
|
2021-05-28 11:09:32 +02:00
|
|
|
.use(auditLog)
|
2020-05-07 15:04:32 +02:00
|
|
|
|
|
|
|
// error handling middleware
|
|
|
|
router.use(async (ctx, next) => {
|
|
|
|
try {
|
|
|
|
await next()
|
|
|
|
} catch (err) {
|
|
|
|
ctx.status = err.status || err.statusCode || 500
|
|
|
|
ctx.body = {
|
|
|
|
message: err.message,
|
|
|
|
status: ctx.status,
|
2021-08-16 22:07:15 +02:00
|
|
|
validationErrors: err.validation,
|
2020-05-07 15:04:32 +02:00
|
|
|
}
|
2021-03-10 18:55:42 +01:00
|
|
|
if (env.NODE_ENV !== "jest") {
|
|
|
|
ctx.log.error(err)
|
|
|
|
console.trace(err)
|
|
|
|
}
|
2020-05-07 15:04:32 +02:00
|
|
|
}
|
|
|
|
})
|
2020-04-08 17:57:27 +02:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
router.get("/health", ctx => (ctx.status = 200))
|
2020-11-10 18:08:02 +01:00
|
|
|
|
2020-05-07 15:04:32 +02:00
|
|
|
// authenticated routes
|
2020-11-16 19:04:44 +01:00
|
|
|
for (let route of mainRoutes) {
|
|
|
|
router.use(route.routes())
|
|
|
|
router.use(route.allowedMethods())
|
|
|
|
}
|
2020-09-29 17:23:34 +02:00
|
|
|
|
2022-02-16 19:23:38 +01:00
|
|
|
router.use(publicRoutes.routes())
|
|
|
|
router.use(publicRoutes.allowedMethods())
|
|
|
|
|
2020-11-16 19:04:44 +01:00
|
|
|
// WARNING - static routes will catch everything else after them this must be last
|
2020-05-07 15:04:32 +02:00
|
|
|
router.use(staticRoutes.routes())
|
|
|
|
router.use(staticRoutes.allowedMethods())
|
2020-04-06 15:05:57 +02:00
|
|
|
|
2020-05-07 15:04:32 +02:00
|
|
|
module.exports = router
|