2022-11-23 08:49:59 +13:00
|
|
|
import Router from "@koa/router"
|
2023-02-14 00:53:01 +13:00
|
|
|
import { auth, middleware } from "@budibase/backend-core"
|
2022-11-23 08:49:59 +13:00
|
|
|
import currentApp from "../middleware/currentapp"
|
|
|
|
import zlib from "zlib"
|
|
|
|
import { mainRoutes, staticRoutes, publicRoutes } from "./routes"
|
|
|
|
import pkg from "../../package.json"
|
|
|
|
import env from "../environment"
|
|
|
|
import { middleware as pro } from "@budibase/pro"
|
|
|
|
export { shutdown } from "./routes/public"
|
2020-05-07 21:53:34 +12:00
|
|
|
const compress = require("koa-compress")
|
2021-03-27 03:56:34 +13:00
|
|
|
|
2022-11-23 08:49:59 +13:00
|
|
|
export const router: Router = new Router()
|
2021-04-22 03:42:44 +12:00
|
|
|
|
2022-03-25 03:24:56 +13:00
|
|
|
router.get("/health", ctx => (ctx.status = 200))
|
|
|
|
router.get("/version", ctx => (ctx.body = pkg.version))
|
|
|
|
|
2023-02-14 00:53:01 +13:00
|
|
|
router.use(middleware.errorHandling)
|
|
|
|
|
2020-05-08 01:04:32 +12:00
|
|
|
router
|
|
|
|
.use(
|
|
|
|
compress({
|
|
|
|
threshold: 2048,
|
|
|
|
gzip: {
|
2021-03-30 03:06:00 +13:00
|
|
|
flush: zlib.constants.Z_SYNC_FLUSH,
|
2020-05-08 01:04:32 +12:00
|
|
|
},
|
|
|
|
deflate: {
|
2021-03-30 03:06:00 +13:00
|
|
|
flush: zlib.constants.Z_SYNC_FLUSH,
|
2020-05-08 01:04:32 +12:00
|
|
|
},
|
2020-05-19 01:58:39 +12:00
|
|
|
br: false,
|
2020-05-07 07:29:47 +12:00
|
|
|
})
|
2020-05-08 01:04:32 +12:00
|
|
|
)
|
2021-09-07 02:48:46 +12:00
|
|
|
// re-direct before any middlewares occur
|
|
|
|
.redirect("/", "/builder")
|
2021-04-29 05:13:21 +12:00
|
|
|
.use(
|
2022-11-27 04:10:41 +13:00
|
|
|
auth.buildAuthMiddleware([], {
|
2021-04-29 05:13:21 +12:00
|
|
|
publicAllowed: true,
|
|
|
|
})
|
|
|
|
)
|
2021-08-05 20:59:08 +12:00
|
|
|
// nothing in the server should allow query string tenants
|
2021-09-07 03:01:45 +12: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(
|
2022-11-27 04:10:41 +13:00
|
|
|
auth.buildTenancyMiddleware([], [], {
|
2021-09-07 03:01:45 +12:00
|
|
|
noTenancyRequired: true,
|
|
|
|
})
|
|
|
|
)
|
2022-03-18 21:01:31 +13:00
|
|
|
.use(pro.licensing())
|
2022-11-23 08:49:59 +13:00
|
|
|
// @ts-ignore
|
2022-09-07 03:24:36 +12:00
|
|
|
.use(currentApp)
|
2022-11-23 08:49:59 +13:00
|
|
|
.use(auth.auditLog)
|
2020-05-08 01:04:32 +12:00
|
|
|
|
|
|
|
// authenticated routes
|
2020-11-17 07:04:44 +13:00
|
|
|
for (let route of mainRoutes) {
|
|
|
|
router.use(route.routes())
|
|
|
|
router.use(route.allowedMethods())
|
|
|
|
}
|
2020-09-30 04:23:34 +13:00
|
|
|
|
2022-02-17 07:23:38 +13:00
|
|
|
router.use(publicRoutes.routes())
|
|
|
|
router.use(publicRoutes.allowedMethods())
|
|
|
|
|
2020-11-17 07:04:44 +13:00
|
|
|
// WARNING - static routes will catch everything else after them this must be last
|
2020-05-08 01:04:32 +12:00
|
|
|
router.use(staticRoutes.routes())
|
|
|
|
router.use(staticRoutes.allowedMethods())
|