2020-02-03 10:24:25 +01:00
|
|
|
const Router = require("@koa/router")
|
2020-05-04 18:13:57 +02:00
|
|
|
const authenticated = require("../middleware/authenticated");
|
2020-05-02 16:29:10 +02:00
|
|
|
const compress = require("koa-compress");
|
|
|
|
const zlib = require("zlib");
|
2020-02-03 10:24:25 +01:00
|
|
|
const { resolve } = require("path")
|
2020-04-28 15:39:35 +02:00
|
|
|
const { homedir } = require("os")
|
2020-02-03 10:24:25 +01:00
|
|
|
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,
|
|
|
|
componentRoutes
|
2020-04-06 15:16:21 +02:00
|
|
|
} = require("./routes");
|
2019-06-28 23:59:27 +02:00
|
|
|
|
2020-04-24 18:28:32 +02:00
|
|
|
module.exports = app => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const router = new Router()
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
router
|
2020-05-02 16:29:10 +02:00
|
|
|
.use(compress({
|
|
|
|
threshold: 2048,
|
|
|
|
gzip: {
|
|
|
|
flush: zlib.Z_SYNC_FLUSH
|
|
|
|
},
|
|
|
|
deflate: {
|
|
|
|
flush: zlib.Z_SYNC_FLUSH,
|
|
|
|
}
|
|
|
|
}))
|
2020-05-04 18:13:57 +02:00
|
|
|
.use(authenticated)
|
2020-04-14 16:14:57 +02:00
|
|
|
.use(async (ctx, next) => {
|
|
|
|
// TODO: temp dev middleware
|
2020-04-20 17:17:11 +02:00
|
|
|
// ctx.sessionId = ctx.session._sessCtx.externalKey
|
|
|
|
// ctx.session.accessed = true
|
2020-05-06 11:33:30 +02:00
|
|
|
ctx.config = {
|
|
|
|
latestPackagesFolder: resolve(homedir(), ".budibase"),
|
|
|
|
secret: "foo"
|
|
|
|
}
|
2020-04-14 16:14:57 +02:00
|
|
|
await next();
|
|
|
|
});
|
2020-04-07 16:12:08 +02:00
|
|
|
|
2020-04-08 17:57:27 +02:00
|
|
|
// error handling middleware
|
|
|
|
router.use(async (ctx, next) => {
|
|
|
|
try {
|
|
|
|
await next();
|
|
|
|
} catch (err) {
|
2020-04-14 16:14:57 +02:00
|
|
|
console.trace(err);
|
2020-04-22 17:35:20 +02:00
|
|
|
ctx.status = err.status || err.statusCode || 500;
|
2020-04-08 17:57:27 +02:00
|
|
|
ctx.body = {
|
|
|
|
message: err.message,
|
2020-04-13 12:47:53 +02:00
|
|
|
status: ctx.status
|
2020-04-08 17:57:27 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-04 18:13:57 +02:00
|
|
|
router.use(authRoutes.routes());
|
|
|
|
router.use(authRoutes.allowedMethods());
|
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
router.use(pageRoutes.routes());
|
|
|
|
router.use(pageRoutes.allowedMethods());
|
|
|
|
|
2020-05-04 18:13:57 +02:00
|
|
|
router.use(viewRoutes.routes());
|
|
|
|
router.use(viewRoutes.allowedMethods());
|
2020-04-14 16:14:57 +02:00
|
|
|
|
2020-05-04 18:13:57 +02:00
|
|
|
router.use(modelRoutes.routes());
|
|
|
|
router.use(modelRoutes.allowedMethods());
|
2020-04-13 12:47:53 +02:00
|
|
|
|
2020-04-08 17:57:27 +02:00
|
|
|
router.use(applicationRoutes.routes());
|
|
|
|
router.use(applicationRoutes.allowedMethods());
|
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
router.use(componentRoutes.routes());
|
|
|
|
router.use(componentRoutes.allowedMethods());
|
|
|
|
|
2020-04-08 17:57:27 +02:00
|
|
|
router.use(clientRoutes.routes());
|
|
|
|
router.use(clientRoutes.allowedMethods());
|
|
|
|
|
2020-05-04 18:13:57 +02:00
|
|
|
router.use(userRoutes.routes());
|
|
|
|
router.use(userRoutes.allowedMethods());
|
2020-04-07 16:12:08 +02:00
|
|
|
|
|
|
|
router.use(recordRoutes.routes());
|
|
|
|
router.use(recordRoutes.allowedMethods());
|
|
|
|
|
2020-04-20 17:17:11 +02:00
|
|
|
router.use(instanceRoutes.routes());
|
|
|
|
router.use(instanceRoutes.allowedMethods());
|
2020-04-07 16:12:08 +02:00
|
|
|
|
2020-05-04 18:13:57 +02:00
|
|
|
router.use(staticRoutes.routes());
|
|
|
|
router.use(staticRoutes.allowedMethods());
|
2020-04-06 15:05:57 +02:00
|
|
|
|
2020-05-04 18:13:57 +02:00
|
|
|
router.redirect("/", "/_builder");
|
2019-06-28 23:59:27 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return router
|
2020-03-24 11:58:15 +01:00
|
|
|
}
|