2020-02-03 22:24:25 +13:00
|
|
|
const Router = require("@koa/router")
|
2021-08-05 20:59:08 +12:00
|
|
|
const { buildAuthMiddleware, auditLog, buildTenancyMiddleware } =
|
|
|
|
require("@budibase/auth").auth
|
2021-04-13 05:31:58 +12:00
|
|
|
const currentApp = require("../middleware/currentapp")
|
2020-05-07 21:53:34 +12:00
|
|
|
const compress = require("koa-compress")
|
|
|
|
const zlib = require("zlib")
|
2021-04-22 03:42:44 +12:00
|
|
|
const { mainRoutes, staticRoutes } = require("./routes")
|
2021-01-28 02:55:46 +13:00
|
|
|
const pkg = require("../../package.json")
|
2021-05-06 04:49:34 +12:00
|
|
|
const env = require("../environment")
|
2021-03-27 03:56:34 +13:00
|
|
|
|
2020-05-08 01:04:32 +12:00
|
|
|
const router = new Router()
|
2021-04-22 03:42:44 +12:00
|
|
|
|
2021-08-05 20:59:08 +12:00
|
|
|
const NO_TENANCY_ENDPOINTS = [
|
|
|
|
{
|
|
|
|
route: "/api/analytics",
|
|
|
|
method: "GET",
|
|
|
|
},
|
2021-09-07 00:36:41 +12:00
|
|
|
{
|
|
|
|
route: "/builder",
|
|
|
|
method: "GET",
|
|
|
|
},
|
|
|
|
// when using this locally there can be pass through, need
|
|
|
|
// to allow all pass through endpoints to go without tenancy
|
|
|
|
{
|
|
|
|
route: "/api/global",
|
|
|
|
method: "ALL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
route: "/api/system",
|
|
|
|
method: "ALL",
|
|
|
|
},
|
2021-08-05 20:59:08 +12:00
|
|
|
]
|
|
|
|
|
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
|
|
|
)
|
|
|
|
.use(async (ctx, next) => {
|
|
|
|
ctx.config = {
|
2020-05-15 02:12:30 +12:00
|
|
|
jwtSecret: env.JWT_SECRET,
|
2020-06-03 23:29:42 +12:00
|
|
|
useAppRootPath: true,
|
2020-04-09 03:57:27 +12:00
|
|
|
}
|
2020-05-08 01:04:32 +12:00
|
|
|
await next()
|
2020-05-07 21:53:34 +12:00
|
|
|
})
|
2021-05-04 22:32:22 +12:00
|
|
|
.use("/health", ctx => (ctx.status = 200))
|
|
|
|
.use("/version", ctx => (ctx.body = pkg.version))
|
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(
|
|
|
|
buildAuthMiddleware(null, {
|
|
|
|
publicAllowed: true,
|
|
|
|
})
|
|
|
|
)
|
2021-08-05 20:59:08 +12:00
|
|
|
// nothing in the server should allow query string tenants
|
|
|
|
.use(buildTenancyMiddleware(null, NO_TENANCY_ENDPOINTS))
|
2021-04-13 05:31:58 +12:00
|
|
|
.use(currentApp)
|
2021-05-28 21:09:32 +12:00
|
|
|
.use(auditLog)
|
2020-05-08 01:04:32 +12: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-17 08:07:15 +12:00
|
|
|
validationErrors: err.validation,
|
2020-05-08 01:04:32 +12:00
|
|
|
}
|
2021-03-11 06:55:42 +13:00
|
|
|
if (env.NODE_ENV !== "jest") {
|
|
|
|
ctx.log.error(err)
|
|
|
|
console.trace(err)
|
|
|
|
}
|
2020-05-08 01:04:32 +12:00
|
|
|
}
|
|
|
|
})
|
2020-04-09 03:57:27 +12:00
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
router.get("/health", ctx => (ctx.status = 200))
|
2020-11-11 06:08:02 +13:00
|
|
|
|
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
|
|
|
|
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())
|
2020-04-07 01:05:57 +12:00
|
|
|
|
2020-05-08 01:04:32 +12:00
|
|
|
module.exports = router
|