1
0
Fork 0
mirror of synced 2024-07-02 04:50:44 +12:00
budibase/packages/server/src/api/controllers/static.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const send = require("koa-send")
const { resolve, join } = require("path")
2020-05-12 02:42:42 +12:00
const {
budibaseAppsDir,
budibaseTempDir,
} = require("../../utilities/budibaseDir")
2020-06-19 03:59:31 +12:00
const setBuilderToken = require("../../utilities/builder/setBuilderToken")
const { ANON_LEVEL_ID } = require("../../utilities/accessLevels")
exports.serveBuilder = async function(ctx) {
let builderPath = resolve(__dirname, "../../../builder")
2020-06-19 03:59:31 +12:00
setBuilderToken(ctx)
2020-05-06 23:17:15 +12:00
await send(ctx, ctx.file, { root: ctx.devPath || builderPath })
}
exports.serveApp = async function(ctx) {
2020-05-03 22:33:20 +12:00
// default to homedir
const appPath = resolve(
2020-05-12 02:42:42 +12:00
budibaseAppsDir(),
2020-05-03 22:33:20 +12:00
ctx.params.appId,
"public",
ctx.isAuthenticated ? "main" : "unauthenticated"
2020-05-07 21:53:34 +12:00
)
// only set the appId cookie for /appId .. we COULD check for valid appIds
// but would like to avoid that DB hit
2020-06-19 03:59:31 +12:00
if (looksLikeAppId(ctx.params.appId) && !ctx.isAuthenticated) {
const anonToken = {
userId: "ANON",
accessLevelId: ANON_LEVEL_ID,
appId: ctx.params.appId,
}
ctx.cookies.set("budibase:token", anonToken, {
path: "/",
httpOnly: false,
})
}
await send(ctx, ctx.file || "index.html", { root: ctx.devPath || appPath })
}
exports.serveAppAsset = async function(ctx) {
// default to homedir
const appPath = resolve(
budibaseAppsDir(),
2020-06-19 03:59:31 +12:00
ctx.user.appId,
"public",
ctx.isAuthenticated ? "main" : "unauthenticated"
)
2020-05-03 22:33:20 +12:00
2020-05-06 23:17:15 +12:00
await send(ctx, ctx.file, { root: ctx.devPath || appPath })
}
exports.serveComponentLibrary = async function(ctx) {
2020-05-06 23:17:15 +12:00
// default to homedir
let componentLibraryPath = resolve(
2020-05-12 02:42:42 +12:00
budibaseAppsDir(),
2020-06-19 03:59:31 +12:00
ctx.user.appId,
2020-05-07 21:53:34 +12:00
"node_modules",
decodeURI(ctx.query.library),
"dist"
2020-05-07 21:53:34 +12:00
)
2020-05-06 23:17:15 +12:00
if (ctx.isDev) {
componentLibraryPath = join(
2020-05-12 02:42:42 +12:00
budibaseTempDir(),
decodeURI(ctx.query.library),
"dist"
2020-05-07 21:53:34 +12:00
)
}
await send(ctx, "/index.js", { root: componentLibraryPath })
}
const looksLikeAppId = appId => /^[0-9a-f]{32}$/.test(appId)