budibase/packages/worker/src/api/index.js

63 lines
1.4 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-26 16:44:28 +02:00
const NO_AUTH_ENDPOINTS = [
2021-05-05 22:56:43 +02:00
"/api/admin/users/init",
"/api/admin/configs/checklist",
2021-04-26 16:44:28 +02:00
"/api/admin/auth",
"/api/admin/auth/google",
"/api/admin/auth/google/callback",
]
const router = new Router()
router
.use(
compress({
threshold: 2048,
gzip: {
2021-03-29 16:06:00 +02:00
flush: zlib.constants.Z_SYNC_FLUSH,
},
deflate: {
2021-03-29 16:06:00 +02:00
flush: zlib.constants.Z_SYNC_FLUSH,
},
br: false,
})
)
2021-05-04 12:32:22 +02:00
.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) => {
2021-05-05 21:59:16 +02:00
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,
}
}
})
2021-05-04 12:32:22 +02:00
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