1
0
Fork 0
mirror of synced 2024-10-01 01:28:51 +13:00
budibase/packages/server/src/api/controllers/static.js

115 lines
3.2 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")
const { S3 } = require("aws-sdk")
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-06-29 21:27:38 +12:00
const mainOrAuth = ctx.isAuthenticated ? "main" : "unauthenticated";
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
)
// only set the appId cookie for /appId .. we COULD check for valid appIds
// but would like to avoid that DB hit
2020-06-20 04:21:24 +12:00
const looksLikeAppId = /^[0-9a-f]{32}$/.test(ctx.params.appId)
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,
appId: ctx.params.appId,
}
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-06-29 21:27:38 +12:00
const { file = "index.html" } = ctx
2020-06-29 21:27:38 +12:00
if (ctx.isCloud) {
2020-07-03 10:22:20 +12:00
const S3_URL = `https://${ctx.params.appId}.app.budi.live/assets/${ctx.params.appId}/${mainOrAuth}/${file}`
console.log("Serving: " + S3_URL)
const response = await fetch(S3_URL)
2020-06-29 21:27:38 +12:00
const body = await response.text()
ctx.body = body
return
2020-06-29 21:27:38 +12:00
}
await send(ctx, file, { root: ctx.devPath || appPath })
}
exports.serveAppAsset = async function(ctx) {
// default to homedir
2020-06-29 21:27:38 +12:00
const mainOrAuth = ctx.isAuthenticated ? "main" : "unauthenticated";
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
// if (ctx.isCloud) {
// const requestUrl = `${S3_URL_PREFIX}/${appId}/public/${mainOrAuth}/${ctx.file || "index.html"}`
// console.log('request url:' , requestUrl)
// const response = await fetch(requestUrl)
// const body = await response.text()
// ctx.body = body
// } else {
2020-06-29 21:27:38 +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
)
}
// if (ctx.isCloud) {
// const appId = ctx.user.appId
// const requestUrl = encodeURI(`${S3_URL_PREFIX}/${appId}/node_modules/${ctx.query.library}/dist/index.js`)
// console.log('request url components: ', requestUrl)
// const response = await fetch(requestUrl)
// const body = await response.text()
// ctx.type = 'application/javascript'
// ctx.body = body;
// return
// }
2020-06-29 21:27:38 +12:00
await send(ctx, "/index.js", { root: componentLibraryPath })
}