1
0
Fork 0
mirror of synced 2024-06-29 03:20:34 +12:00
budibase/packages/server/src/api/index.js

102 lines
2.2 KiB
JavaScript
Raw Normal View History

const Router = require("@koa/router")
2020-05-07 21:53:34 +12:00
const authenticated = require("../middleware/authenticated")
const compress = require("koa-compress")
const zlib = require("zlib")
2020-05-12 02:42:42 +12:00
const { budibaseAppsDir } = require("../utilities/budibaseDir")
const {
2020-05-05 04:13:57 +12:00
authRoutes,
2020-04-07 01:05:57 +12:00
pageRoutes,
userRoutes,
2020-05-05 04:13:57 +12:00
instanceRoutes,
clientRoutes,
applicationRoutes,
modelRoutes,
viewRoutes,
staticRoutes,
2020-05-07 21:53:34 +12:00
componentRoutes,
2020-05-22 01:43:49 +12:00
workflowRoutes,
2020-05-22 01:31:23 +12:00
accesslevelRoutes,
2020-05-07 21:53:34 +12:00
} = require("./routes")
2019-06-29 09:59:27 +12:00
2020-05-08 01:04:32 +12:00
const router = new Router()
2020-05-15 02:12:30 +12:00
const env = require("../environment")
2020-05-08 01:04:32 +12:00
router
.use(
compress({
threshold: 2048,
gzip: {
flush: zlib.Z_SYNC_FLUSH,
},
deflate: {
flush: zlib.Z_SYNC_FLUSH,
},
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-12 02:42:42 +12:00
latestPackagesFolder: budibaseAppsDir(),
2020-05-15 02:12:30 +12:00
jwtSecret: env.JWT_SECRET,
2020-04-09 03:57:27 +12:00
}
2020-05-15 02:12:30 +12:00
ctx.isDev = env.NODE_ENV !== "production" && env.NODE_ENV !== "jest"
2020-05-08 01:04:32 +12:00
await next()
2020-05-07 21:53:34 +12:00
})
2020-05-08 01:04:32 +12:00
.use(authenticated)
// error handling middleware
router.use(async (ctx, next) => {
try {
await next()
} catch (err) {
2020-05-19 02:46:38 +12:00
ctx.log.error(err)
2020-05-08 01:04:32 +12:00
ctx.status = err.status || err.statusCode || 500
ctx.body = {
message: err.message,
status: ctx.status,
}
}
})
2020-04-09 03:57:27 +12:00
2020-05-08 01:04:32 +12:00
router.use(authRoutes.routes())
router.use(authRoutes.allowedMethods())
2020-05-05 04:13:57 +12:00
2020-05-08 01:04:32 +12:00
// authenticated routes
router.use(viewRoutes.routes())
router.use(viewRoutes.allowedMethods())
2020-05-08 01:04:32 +12:00
router.use(modelRoutes.routes())
router.use(modelRoutes.allowedMethods())
2020-04-13 22:47:53 +12:00
2020-05-08 01:04:32 +12:00
router.use(userRoutes.routes())
router.use(userRoutes.allowedMethods())
2020-04-08 02:12:08 +12:00
2020-05-08 01:04:32 +12:00
router.use(instanceRoutes.routes())
router.use(instanceRoutes.allowedMethods())
2020-05-21 04:02:46 +12:00
router.use(workflowRoutes.routes())
router.use(workflowRoutes.allowedMethods())
2020-05-08 01:04:32 +12:00
// end auth routes
2020-05-07 07:29:47 +12:00
2020-05-08 01:04:32 +12:00
router.use(pageRoutes.routes())
router.use(pageRoutes.allowedMethods())
2020-05-07 07:29:47 +12:00
2020-05-08 01:04:32 +12:00
router.use(applicationRoutes.routes())
router.use(applicationRoutes.allowedMethods())
2020-05-07 07:29:47 +12:00
2020-05-08 01:04:32 +12:00
router.use(componentRoutes.routes())
router.use(componentRoutes.allowedMethods())
2020-05-07 07:29:47 +12:00
2020-05-08 01:04:32 +12:00
router.use(clientRoutes.routes())
router.use(clientRoutes.allowedMethods())
2020-04-08 02:12:08 +12:00
2020-05-22 01:31:23 +12:00
router.use(accesslevelRoutes.routes())
router.use(accesslevelRoutes.allowedMethods())
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
router.redirect("/", "/_builder")
2019-06-29 09:59:27 +12:00
2020-05-08 01:04:32 +12:00
module.exports = router