Serve web build from Slide Factory API

This commit is contained in:
Codex 2026-06-09 10:35:52 -07:00
parent a010792eef
commit 84adc53bd0

View file

@ -1,3 +1,4 @@
import { existsSync } from "node:fs";
import { access, mkdir, writeFile } from "node:fs/promises"; import { access, mkdir, writeFile } from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
@ -27,6 +28,7 @@ const CreateDeckRequestSchema = z.object({
const app = express(); const app = express();
const port = Number(process.env.SLIDE_FACTORY_PORT || 3025); const port = Number(process.env.SLIDE_FACTORY_PORT || 3025);
const outputDir = path.resolve(process.env.SLIDE_FACTORY_OUTPUT_DIR || "outputs"); const outputDir = path.resolve(process.env.SLIDE_FACTORY_OUTPUT_DIR || "outputs");
const webDistDir = resolveFromRepoRoot("apps/web/dist");
const upload = multer({ const upload = multer({
storage: multer.memoryStorage(), storage: multer.memoryStorage(),
limits: { limits: {
@ -84,6 +86,17 @@ app.post("/api/decks/from-source", upload.single("source"), async (req, res, nex
} }
}); });
if (existsSync(webDistDir)) {
app.use(express.static(webDistDir));
app.get("*", (req, res, next) => {
if (req.path.startsWith("/api/") || req.path.startsWith("/outputs/")) {
next();
return;
}
res.sendFile(path.join(webDistDir, "index.html"));
});
}
app.use((error: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => { app.use((error: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
console.error(error); console.error(error);
res.status(400).json({ res.status(400).json({
@ -99,14 +112,17 @@ async function resolveStylePath(styleId: string): Promise<string> {
const direct = path.resolve("styles", styleId); const direct = path.resolve("styles", styleId);
if (await exists(path.join(direct, "theme.json"))) return direct; if (await exists(path.join(direct, "theme.json"))) return direct;
const moduleDir = path.dirname(fileURLToPath(import.meta.url)); const fromRepoRoot = resolveFromRepoRoot("styles", styleId);
const repoRoot = path.resolve(moduleDir, "../../..");
const fromRepoRoot = path.join(repoRoot, "styles", styleId);
if (await exists(path.join(fromRepoRoot, "theme.json"))) return fromRepoRoot; if (await exists(path.join(fromRepoRoot, "theme.json"))) return fromRepoRoot;
return direct; return direct;
} }
function resolveFromRepoRoot(...parts: string[]): string {
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
return path.resolve(moduleDir, "../../..", ...parts);
}
async function createDeckArtifacts(options: { async function createDeckArtifacts(options: {
source: SourceDocument; source: SourceDocument;
instructions?: string; instructions?: string;