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

82 lines
2.2 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"
2024-02-02 22:32:07 +13:00
import cleanup from "../middleware/cleanup"
import zlib from "zlib"
import { mainRoutes, staticRoutes, publicRoutes } from "./routes"
import { middleware as pro } from "@budibase/pro"
2023-12-14 01:30:11 +13:00
import { apiEnabled, automationsEnabled } from "../features"
2023-11-28 03:40:37 +13:00
import migrations from "../middleware/appMigrations"
2023-12-14 00:10:58 +13:00
import { automationQueue } from "../automations"
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()
router.get("/health", async ctx => {
if (automationsEnabled()) {
if (!(await automationQueue.isReady())) {
ctx.status = 503
return
}
}
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)
// only add the routes if they are enabled
if (apiEnabled()) {
router
.use(
compress({
threshold: 2048,
gzip: {
flush: zlib.constants.Z_SYNC_FLUSH,
},
deflate: {
flush: zlib.constants.Z_SYNC_FLUSH,
},
br: false,
})
)
// re-direct before any middlewares occur
.redirect("/", "/builder")
.use(
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(
auth.buildTenancyMiddleware([], [], {
noTenancyRequired: true,
})
)
.use(pro.licensing())
// @ts-ignore
.use(currentApp)
.use(auth.auditLog)
// @ts-ignore
.use(migrations)
2024-02-02 22:32:07 +13:00
// @ts-ignore
.use(cleanup)
2020-05-08 01:04:32 +12:00
// authenticated routes
for (let route of mainRoutes) {
router.use(route.routes())
router.use(route.allowedMethods())
}
router.use(publicRoutes.routes())
router.use(publicRoutes.allowedMethods())
2022-02-17 07:23:38 +13:00
// WARNING - static routes will catch everything else after them this must be last
router.use(staticRoutes.routes())
router.use(staticRoutes.allowedMethods())
}