2020-05-07 21:53:34 +12:00
|
|
|
const Router = require("@koa/router")
|
|
|
|
const controller = require("../controllers/static")
|
2020-05-12 02:42:42 +12:00
|
|
|
const { budibaseTempDir } = require("../../utilities/budibaseDir")
|
2020-09-16 03:22:13 +12:00
|
|
|
const authorized = require("../../middleware/authorized")
|
2021-04-02 02:38:31 +13:00
|
|
|
const {
|
|
|
|
BUILDER,
|
|
|
|
PermissionTypes,
|
|
|
|
PermissionLevels,
|
2021-05-15 02:43:41 +12:00
|
|
|
} = require("@budibase/auth/permissions")
|
2020-10-08 05:56:47 +13:00
|
|
|
const usage = require("../../middleware/usageQuota")
|
2021-03-25 09:44:52 +13:00
|
|
|
const env = require("../../environment")
|
2020-04-15 02:14:57 +12:00
|
|
|
|
2020-05-07 21:53:34 +12:00
|
|
|
const router = Router()
|
2020-04-15 02:14:57 +12:00
|
|
|
|
2021-03-11 06:55:42 +13:00
|
|
|
/* istanbul ignore next */
|
2020-05-18 17:40:29 +12:00
|
|
|
router.param("file", async (file, ctx, next) => {
|
|
|
|
ctx.file = file && file.includes(".") ? file : "index.html"
|
2021-03-26 05:08:09 +13:00
|
|
|
if (!ctx.file.startsWith("budibase-client")) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
// test serves from require
|
|
|
|
if (env.isTest()) {
|
|
|
|
ctx.devPath = require.resolve("@budibase/client").split(ctx.file)[0]
|
|
|
|
} else if (env.isDev()) {
|
|
|
|
// Serving the client library from your local dir in dev
|
2020-05-18 17:40:29 +12:00
|
|
|
ctx.devPath = budibaseTempDir()
|
|
|
|
}
|
2021-03-26 05:08:09 +13:00
|
|
|
return next()
|
2020-05-18 17:40:29 +12:00
|
|
|
})
|
2020-05-06 23:17:15 +12:00
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
// only used in development for retrieving the client library,
|
|
|
|
// in production the client lib is always stored in the object store.
|
2021-04-02 00:48:38 +13:00
|
|
|
if (env.isDev()) {
|
2021-04-02 02:11:58 +13:00
|
|
|
router.get("/api/assets/client", controller.serveClientLibrary)
|
2021-04-02 00:48:38 +13:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:40:29 +12:00
|
|
|
router
|
2021-04-01 07:55:55 +13:00
|
|
|
// TODO: for now this builder endpoint is not authorized/secured, will need to be
|
|
|
|
.get("/builder/:file*", controller.serveBuilder)
|
2021-03-16 01:10:21 +13:00
|
|
|
.post("/api/attachments/process", authorized(BUILDER), controller.uploadFile)
|
2021-04-02 02:38:31 +13:00
|
|
|
.post(
|
|
|
|
"/api/attachments/upload",
|
|
|
|
authorized(PermissionTypes.TABLE, PermissionLevels.WRITE),
|
|
|
|
usage,
|
|
|
|
controller.uploadFile
|
|
|
|
)
|
2020-06-13 07:42:55 +12:00
|
|
|
.get("/componentlibrary", controller.serveComponentLibrary)
|
2021-04-02 02:38:31 +13:00
|
|
|
// TODO: this likely needs to be secured in some way
|
2020-06-13 07:42:55 +12:00
|
|
|
.get("/:appId/:path*", controller.serveApp)
|
2020-04-15 02:14:57 +12:00
|
|
|
|
2020-05-07 21:53:34 +12:00
|
|
|
module.exports = router
|