1
0
Fork 0
mirror of synced 2024-09-27 06:42:03 +12:00
budibase/packages/worker/src/api/index.js

74 lines
1.5 KiB
JavaScript
Raw Normal View History

const Router = require("@koa/router")
const compress = require("koa-compress")
const zlib = require("zlib")
const { routes } = require("./routes")
const { buildAuthMiddleware } = require("@budibase/auth").auth
2021-04-27 02:44:28 +12:00
const NO_AUTH_ENDPOINTS = [
{
route: "/api/admin/users/first",
method: "POST",
},
{
route: "/api/admin/auth",
method: "POST",
},
{
route: "/api/admin/auth/google",
method: "GET",
},
{
route: "/api/admin/auth/google/callback",
method: "GET",
},
2021-04-27 02:44:28 +12:00
]
const router = new Router()
router
.use(
compress({
threshold: 2048,
gzip: {
2021-03-30 03:06:00 +13:00
flush: zlib.constants.Z_SYNC_FLUSH,
},
deflate: {
2021-03-30 03:06:00 +13:00
flush: zlib.constants.Z_SYNC_FLUSH,
},
br: false,
})
)
.use("/health", ctx => (ctx.status = 200))
.use(buildAuthMiddleware(NO_AUTH_ENDPOINTS))
// for now no public access is allowed to worker (bar health check)
.use((ctx, next) => {
if (!ctx.isAuthenticated) {
ctx.throw(403, "Unauthorized - no public worker access")
}
return next()
})
// error handling middleware
router.use(async (ctx, next) => {
try {
await next()
} catch (err) {
ctx.log.error(err)
ctx.status = err.status || err.statusCode || 500
ctx.body = {
message: err.message,
status: ctx.status,
}
}
})
router.get("/health", ctx => (ctx.status = 200))
// authenticated routes
for (let route of routes) {
router.use(route.routes())
router.use(route.allowedMethods())
}
module.exports = router