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

97 lines
2.1 KiB
JavaScript
Raw Normal View History

const Router = require("@koa/router")
2020-05-07 11:53:34 +02:00
const authenticated = require("../middleware/authenticated")
const compress = require("koa-compress")
const zlib = require("zlib")
const { resolve } = require("path")
const { homedir } = require("os")
const {
2020-05-04 18:13:57 +02:00
authRoutes,
2020-04-06 15:05:57 +02:00
pageRoutes,
userRoutes,
2020-05-04 18:13:57 +02:00
recordRoutes,
instanceRoutes,
clientRoutes,
applicationRoutes,
modelRoutes,
viewRoutes,
staticRoutes,
2020-05-07 11:53:34 +02:00
componentRoutes,
} = require("./routes")
2019-06-28 23:59:27 +02:00
2020-05-07 15:04:32 +02:00
const router = new Router()
router
.use(
compress({
threshold: 2048,
gzip: {
flush: zlib.Z_SYNC_FLUSH,
},
deflate: {
flush: zlib.Z_SYNC_FLUSH,
},
2020-05-06 21:29:47 +02:00
})
2020-05-07 15:04:32 +02:00
)
.use(async (ctx, next) => {
ctx.config = {
latestPackagesFolder: resolve(homedir(), ".budibase"),
jwtSecret: process.env.JWT_SECRET,
2020-04-08 17:57:27 +02:00
}
2020-05-07 15:04:32 +02:00
ctx.isDev = process.env.NODE_ENV !== "production"
await next()
2020-05-07 11:53:34 +02:00
})
2020-05-07 15:04:32 +02:00
.use(authenticated)
// error handling middleware
router.use(async (ctx, next) => {
try {
await next()
} catch (err) {
console.trace(err)
ctx.status = err.status || err.statusCode || 500
ctx.body = {
message: err.message,
status: ctx.status,
}
}
})
2020-04-08 17:57:27 +02:00
2020-05-07 15:04:32 +02:00
router.use(authRoutes.routes())
router.use(authRoutes.allowedMethods())
2020-05-04 18:13:57 +02:00
2020-05-07 15:04:32 +02:00
// authenticated routes
router.use(viewRoutes.routes())
router.use(viewRoutes.allowedMethods())
2020-05-07 15:04:32 +02:00
router.use(modelRoutes.routes())
router.use(modelRoutes.allowedMethods())
2020-04-13 12:47:53 +02:00
2020-05-07 15:04:32 +02:00
router.use(userRoutes.routes())
router.use(userRoutes.allowedMethods())
2020-04-07 16:12:08 +02:00
2020-05-07 15:04:32 +02:00
router.use(recordRoutes.routes())
router.use(recordRoutes.allowedMethods())
2020-04-07 16:12:08 +02:00
2020-05-07 15:04:32 +02:00
router.use(instanceRoutes.routes())
router.use(instanceRoutes.allowedMethods())
// end auth routes
2020-05-06 21:29:47 +02:00
2020-05-07 15:04:32 +02:00
router.use(pageRoutes.routes())
router.use(pageRoutes.allowedMethods())
2020-05-06 21:29:47 +02:00
2020-05-07 15:04:32 +02:00
router.use(applicationRoutes.routes())
router.use(applicationRoutes.allowedMethods())
2020-05-06 21:29:47 +02:00
2020-05-07 15:04:32 +02:00
router.use(componentRoutes.routes())
router.use(componentRoutes.allowedMethods())
2020-05-06 21:29:47 +02:00
2020-05-07 15:04:32 +02:00
router.use(clientRoutes.routes())
router.use(clientRoutes.allowedMethods())
2020-04-07 16:12:08 +02:00
2020-05-07 15:04:32 +02:00
router.use(staticRoutes.routes())
router.use(staticRoutes.allowedMethods())
2020-04-06 15:05:57 +02:00
2020-05-07 15:04:32 +02:00
router.redirect("/", "/_builder")
2019-06-28 23:59:27 +02:00
2020-05-07 15:04:32 +02:00
module.exports = router