diff --git a/apps/api/src/app.ts b/apps/api/src/app.ts index 7623b3a..2f2fb95 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -1,5 +1,5 @@ import { randomBytes, randomUUID } from "node:crypto"; -import { createReadStream, readFileSync } from "node:fs"; +import { createReadStream, existsSync, readFileSync } from "node:fs"; import { resolve } from "node:path"; import { @@ -272,6 +272,10 @@ const supportGateDirectory = resolve(process.env.DADA_SUPPORT_GATE_ROOT ?? "apps const supportGateHtml = readFileSync(resolve(supportGateDirectory, "index.html"), "utf8"); const supportGateCss = readFileSync(resolve(supportGateDirectory, "support-gate.css"), "utf8"); const supportGateJavaScript = readFileSync(resolve(supportGateDirectory, "support-gate.js"), "utf8"); +const productWebRoot = resolve(process.env.DADA_WEB_ROOT ?? "apps/web/dist"); +const productIndexHtml = existsSync(resolve(productWebRoot, "index.html")) + ? readFileSync(resolve(productWebRoot, "index.html"), "utf8") + : undefined; const clientHints = "Sec-CH-UA, Sec-CH-UA-Full-Version-List, Sec-CH-UA-Platform"; const contentSecurityPolicy = [ "default-src 'self'", @@ -903,9 +907,15 @@ export async function createApp(options: CreateAppOptions = {}) { for (const route of ["/", "/app", "/app/*", "/admin", "/admin/*"]) { app.get(route, { schema: { hide: true } }, async (_request, reply) => { reply.type("text/html; charset=utf-8"); - return supportGateHtml; + return productIndexHtml ?? supportGateHtml; }); } + app.get("/assets/*", { schema: { hide: true } }, async (request, reply) => { + const relativePath = decodeURIComponent(request.url.split("?", 1)[0]!.slice("/assets/".length)); + const assetPath = resolve(productWebRoot, "assets", relativePath); + if (!assetPath.startsWith(resolve(productWebRoot, "assets")) || !existsSync(assetPath)) return reply.code(404).send(); + return reply.send(readFileSync(assetPath)); + }); app.get("/support-gate.css", { schema: { hide: true } }, async (_request, reply) => { reply.type("text/css; charset=utf-8"); return supportGateCss;