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

70 lines
1.7 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")
const env = require("../../environment")
exports.serveBuilder = async function(ctx) {
let builderPath = resolve(__dirname, "../../../builder")
ctx.cookies.set("builder:token", env.ADMIN_SECRET)
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
if (looksLikeAppId(ctx.params.appId)) {
ctx.cookies.set("budibase:appid", ctx.params.appId, {
path: "/",
httpOnly: false,
expires: new Date(2099, 1, 1),
})
}
await send(ctx, ctx.file || "index.html", { root: ctx.devPath || appPath })
}
exports.serveAppAsset = async function(ctx) {
// default to homedir
const appPath = resolve(
budibaseAppsDir(),
ctx.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(),
ctx.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)