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

110 lines
2.8 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")
2020-06-20 03:59:46 +12:00
const jwt = require("jsonwebtoken")
2020-06-29 21:27:38 +12:00
const fetch = require("node-fetch")
exports.serveBuilder = async function(ctx) {
let builderPath = resolve(__dirname, "../../../builder")
2020-06-20 03:59:46 +12:00
if (ctx.file === "index.html") {
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-07-08 08:29:20 +12:00
const mainOrAuth = ctx.isAuthenticated ? "main" : "unauthenticated"
2020-06-29 21:27:38 +12:00
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",
2020-06-29 21:27:38 +12:00
mainOrAuth
2020-05-07 21:53:34 +12:00
)
2020-07-08 04:47:18 +12:00
let appId = ctx.params.appId
if (process.env.CLOUD) {
appId = ctx.subdomains[1]
}
2020-07-08 08:29:20 +12:00
// only set the appId cookie for /appId .. we COULD check for valid appIds
// but would like to avoid that DB hit
2020-07-08 04:47:18 +12:00
const looksLikeAppId = /^[0-9a-f]{32}$/.test(appId)
2020-06-20 04:21:24 +12:00
if (looksLikeAppId && !ctx.isAuthenticated) {
2020-06-20 03:59:46 +12:00
const anonUser = {
2020-06-19 03:59:31 +12:00
userId: "ANON",
accessLevelId: ANON_LEVEL_ID,
2020-07-08 04:47:18 +12:00
appId,
2020-06-19 03:59:31 +12:00
}
2020-06-20 03:59:46 +12:00
const anonToken = jwt.sign(anonUser, ctx.config.jwtSecret)
2020-06-19 03:59:31 +12:00
ctx.cookies.set("budibase:token", anonToken, {
path: "/",
httpOnly: false,
})
}
2020-07-07 06:43:40 +12:00
if (process.env.CLOUD) {
2020-07-08 08:29:20 +12:00
const S3_URL = `https://${appId}.app.budi.live/assets/${appId}/${mainOrAuth}/${ctx.file ||
"index.production.html"}`
const response = await fetch(S3_URL)
2020-06-29 21:27:38 +12:00
const body = await response.text()
2020-07-08 08:29:20 +12:00
ctx.body = body
return
2020-06-29 21:27:38 +12:00
}
2020-07-07 06:43:40 +12:00
await send(ctx, ctx.file || "index.html", { root: ctx.devPath || appPath })
}
exports.serveAppAsset = async function(ctx) {
// default to homedir
2020-07-08 08:29:20 +12:00
const mainOrAuth = ctx.isAuthenticated ? "main" : "unauthenticated"
2020-06-29 21:27:38 +12:00
const appPath = resolve(
budibaseAppsDir(),
2020-06-19 03:59:31 +12:00
ctx.user.appId,
"public",
2020-06-29 21:27:38 +12:00
mainOrAuth
)
2020-05-03 22:33:20 +12:00
2020-07-07 06:43:40 +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
)
}
2020-07-07 06:43:40 +12:00
if (process.env.CLOUD) {
const appId = ctx.user.appId
2020-07-08 08:29:20 +12:00
const S3_URL = encodeURI(
`https://${appId}.app.budi.live/assets/componentlibrary/${ctx.query.library}/dist/index.js`
)
2020-07-07 06:43:40 +12:00
const response = await fetch(S3_URL)
const body = await response.text()
2020-07-08 08:29:20 +12:00
ctx.type = "application/javascript"
ctx.body = body
2020-07-07 06:43:40 +12:00
return
}
2020-06-29 21:27:38 +12:00
await send(ctx, "/index.js", { root: componentLibraryPath })
}