1
0
Fork 0
mirror of synced 2024-09-21 20:01:32 +12:00
budibase/packages/server/src/api/index.ts

63 lines
1.7 KiB
TypeScript
Raw Normal View History

import Router from "@koa/router"
2023-04-18 00:53:00 +12:00
import { auth, middleware, env as envCore } from "@budibase/backend-core"
import currentApp from "../middleware/currentapp"
import zlib from "zlib"
import { mainRoutes, staticRoutes, publicRoutes } from "./routes"
import { middleware as pro } from "@budibase/pro"
2023-11-21 09:52:29 +13:00
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
export const router: Router = new Router()
2022-03-25 03:24:56 +13:00
router.get("/health", ctx => (ctx.status = 200))
2023-04-18 00:53:00 +12:00
router.get("/version", ctx => (ctx.body = envCore.VERSION))
2022-03-25 03:24:56 +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
)
// re-direct before any middlewares occur
.redirect("/", "/builder")
.use(
2022-11-27 04:10:41 +13:00
auth.buildAuthMiddleware([], {
publicAllowed: true,
})
)
// nothing in the server should allow query string tenants
// 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([], [], {
noTenancyRequired: true,
})
)
.use(pro.licensing())
// @ts-ignore
.use(currentApp)
.use(auth.auditLog)
2020-05-08 01:04:32 +12:00
// authenticated routes
for (let route of mainRoutes) {
router.use(route.routes())
router.use(route.allowedMethods())
}
2022-02-17 07:23:38 +13:00
router.use(publicRoutes.routes())
router.use(publicRoutes.allowedMethods())
// 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())