2020-02-03 10:24:25 +01:00
|
|
|
const Router = require("@koa/router")
|
|
|
|
const session = require("./session")
|
|
|
|
const StatusCodes = require("../utilities/statusCodes")
|
|
|
|
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 send = require("koa-send")
|
2020-03-10 11:05:09 +01:00
|
|
|
const routeHandlers = require("./routeHandlers")
|
2020-02-03 10:24:25 +01:00
|
|
|
const {
|
|
|
|
componentLibraryInfo,
|
2020-04-06 15:16:21 +02:00
|
|
|
} = require("../utilities/builder")
|
2020-04-06 15:05:57 +02:00
|
|
|
const {
|
|
|
|
componentRoutes,
|
|
|
|
appsRoutes,
|
|
|
|
pageRoutes,
|
|
|
|
userRoutes,
|
|
|
|
authenticatedRoutes
|
2020-04-06 15:16:21 +02:00
|
|
|
} = require("./routes");
|
2019-06-28 23:59:27 +02:00
|
|
|
|
2020-04-07 16:12:08 +02:00
|
|
|
const recordRoutes = require("./routes/neo/record");
|
2020-04-20 17:17:11 +02:00
|
|
|
const instanceRoutes = require("./routes/neo/instance");
|
2020-04-07 18:38:01 +02:00
|
|
|
const neoUserRoutes = require("./routes/neo/user");
|
2020-04-08 17:57:27 +02:00
|
|
|
const clientRoutes = require("./routes/neo/client");
|
|
|
|
const applicationRoutes = require("./routes/neo/application");
|
2020-04-13 12:47:53 +02:00
|
|
|
const modelsRoutes = require("./routes/neo/model");
|
2020-04-13 17:22:30 +02:00
|
|
|
const viewsRoutes = require("./routes/neo/view");
|
2020-04-14 16:14:57 +02:00
|
|
|
const staticRoutes = require("./routes/neo/static");
|
2020-04-07 16:12:08 +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-04-28 15:39:35 +02:00
|
|
|
// .use(session(app))
|
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-04-14 16:14:57 +02:00
|
|
|
ctx.isAuthenticated = true;
|
2020-04-28 15:39:35 +02:00
|
|
|
ctx.config = { latestPackagesFolder: resolve(homedir(), ".budibase") }
|
2020-04-14 16:14:57 +02:00
|
|
|
await next();
|
|
|
|
});
|
2020-04-07 16:12:08 +02:00
|
|
|
// .use(async (ctx, next) => {
|
2020-04-20 17:17:11 +02:00
|
|
|
// ctx.sessionId = ctx.session._sessCtx.externalKey
|
|
|
|
// ctx.session.accessed = true
|
|
|
|
// ctx.config = config
|
2020-04-07 16:12:08 +02:00
|
|
|
|
|
|
|
// const pathParts = ctx.path.split("/")
|
|
|
|
|
|
|
|
// if (pathParts.length < 2) {
|
|
|
|
// ctx.throw(StatusCodes.NOT_FOUND, "App Name not declared")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const appname = pathParts[1]
|
|
|
|
// ctx.set("x-bbappname", appname)
|
|
|
|
|
|
|
|
// if (appname === "_builder") {
|
|
|
|
// if (!config.dev) {
|
|
|
|
// ctx.response.status = StatusCodes.FORBIDDEN
|
|
|
|
// ctx.body = "run in dev mode to access builder"
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Builder URLs should have admin access to the API
|
|
|
|
// if (ctx.path.startsWith("/_builder/instance/_master")) {
|
|
|
|
// const {
|
|
|
|
// instance,
|
|
|
|
// publicPath,
|
|
|
|
// sharedPath,
|
|
|
|
// } = await ctx.master.getFullAccessApiForMaster()
|
|
|
|
// ctx.instance = instance
|
|
|
|
// ctx.publicPath = publicPath
|
|
|
|
// ctx.sharedPath = sharedPath
|
|
|
|
// ctx.isAuthenticated = !!ctx.instance
|
|
|
|
// } else if (ctx.path.startsWith("/_builder/instance")) {
|
|
|
|
// const builderAppName = pathParts[3]
|
|
|
|
// const instanceId = pathParts[4]
|
|
|
|
// const {
|
|
|
|
// bbInstance,
|
|
|
|
// publicPath,
|
|
|
|
// sharedPath,
|
|
|
|
// } = await ctx.master.getFullAccessApiForInstanceId(
|
|
|
|
// builderAppName,
|
|
|
|
// instanceId
|
|
|
|
// )
|
|
|
|
// ctx.instance = bbInstance
|
|
|
|
// ctx.publicPath = publicPath
|
|
|
|
// ctx.sharedPath = sharedPath
|
|
|
|
// ctx.isAuthenticated = !!ctx.instance
|
|
|
|
// }
|
|
|
|
|
|
|
|
// await next()
|
|
|
|
// } else {
|
|
|
|
// const instance = await ctx.master.getInstanceApiForSession(
|
|
|
|
// appname,
|
|
|
|
// ctx.sessionId
|
|
|
|
// )
|
|
|
|
|
|
|
|
// ctx.instance = instance.instance
|
|
|
|
// ctx.publicPath = instance.publicPath
|
|
|
|
// ctx.sharedPath = instance.sharedPath
|
|
|
|
// ctx.isAuthenticated = !!instance.instance
|
|
|
|
|
|
|
|
// await next()
|
|
|
|
// }
|
|
|
|
// })
|
2020-03-26 12:19:21 +01:00
|
|
|
|
2020-04-06 15:16:21 +02:00
|
|
|
router
|
2020-04-14 16:14:57 +02:00
|
|
|
// .get("/_builder", async ctx => {
|
|
|
|
// await send(ctx, "/index.html", { root: builderPath })
|
|
|
|
// })
|
2020-04-06 15:16:21 +02:00
|
|
|
.get("/_builder/:appname/componentlibrary", async ctx => {
|
|
|
|
const info = await componentLibraryInfo(
|
|
|
|
ctx.config,
|
|
|
|
ctx.params.appname,
|
|
|
|
ctx.query.lib
|
|
|
|
)
|
|
|
|
await send(ctx, info.components._lib || "index.js", { root: info.libDir })
|
|
|
|
})
|
2020-04-14 16:14:57 +02:00
|
|
|
// .get("/_builder/*", async (ctx, next) => {
|
|
|
|
// const path = ctx.path.replace("/_builder", "")
|
|
|
|
|
|
|
|
// const isFile = new RegExp(/(.+\..{1,5})/g).test(path)
|
|
|
|
|
|
|
|
// if (path.startsWith("/api/") || path.startsWith("/instance/")) {
|
|
|
|
// await next()
|
|
|
|
// } else if (isFile) {
|
|
|
|
// await send(ctx, path, { root: builderPath })
|
|
|
|
// } else {
|
|
|
|
// await send(ctx, "/index.html", { root: builderPath })
|
|
|
|
// }
|
|
|
|
// })
|
2020-04-07 16:12:08 +02:00
|
|
|
|
2020-04-07 18:38:01 +02:00
|
|
|
// Neo
|
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-04-20 17:17:11 +02:00
|
|
|
// Legacy Routes
|
|
|
|
router.use(userRoutes.routes());
|
|
|
|
router.use(userRoutes.allowedMethods());
|
|
|
|
router.use(appsRoutes.routes())
|
|
|
|
router.use(appsRoutes.allowedMethods());
|
|
|
|
router.use(componentRoutes.routes());
|
|
|
|
router.use(componentRoutes.allowedMethods());
|
|
|
|
router.use(pageRoutes.routes());
|
|
|
|
router.use(pageRoutes.allowedMethods());
|
|
|
|
|
|
|
|
// Neo Routes
|
2020-04-14 16:14:57 +02:00
|
|
|
router.use(staticRoutes.routes());
|
|
|
|
router.use(staticRoutes.allowedMethods());
|
|
|
|
|
2020-04-13 17:22:30 +02:00
|
|
|
router.use(viewsRoutes.routes());
|
|
|
|
router.use(viewsRoutes.allowedMethods());
|
|
|
|
|
2020-04-13 12:47:53 +02:00
|
|
|
router.use(modelsRoutes.routes());
|
|
|
|
router.use(modelsRoutes.allowedMethods());
|
|
|
|
|
2020-04-08 17:57:27 +02:00
|
|
|
router.use(applicationRoutes.routes());
|
|
|
|
router.use(applicationRoutes.allowedMethods());
|
|
|
|
|
|
|
|
router.use(clientRoutes.routes());
|
|
|
|
router.use(clientRoutes.allowedMethods());
|
|
|
|
|
2020-04-07 18:38:01 +02:00
|
|
|
router.use(neoUserRoutes.routes());
|
|
|
|
router.use(neoUserRoutes.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 18:38:01 +02:00
|
|
|
// end of Neo
|
|
|
|
|
2020-04-07 16:12:08 +02:00
|
|
|
// router
|
2020-04-14 16:14:57 +02:00
|
|
|
// .get("/:appname", async ctx => {
|
|
|
|
// await send(ctx, "/index.html", { root: ctx.publicPath })
|
|
|
|
// })
|
2020-04-07 16:12:08 +02:00
|
|
|
// .get("/:appname/*", routeHandlers.appDefault)
|
|
|
|
// .get("/_builder/instance/:appname/:instanceid/*", routeHandlers.appDefault)
|
|
|
|
|
2020-04-06 15:05:57 +02:00
|
|
|
|
|
|
|
router.use(authenticatedRoutes.routes());
|
|
|
|
router.use(authenticatedRoutes.allowedMethods());
|
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
|
|
|
}
|