Serve web build from Slide Factory API
This commit is contained in:
parent
a010792eef
commit
84adc53bd0
1 changed files with 19 additions and 3 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { existsSync } from "node:fs";
|
||||
import { access, mkdir, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
|
@ -27,6 +28,7 @@ const CreateDeckRequestSchema = z.object({
|
|||
const app = express();
|
||||
const port = Number(process.env.SLIDE_FACTORY_PORT || 3025);
|
||||
const outputDir = path.resolve(process.env.SLIDE_FACTORY_OUTPUT_DIR || "outputs");
|
||||
const webDistDir = resolveFromRepoRoot("apps/web/dist");
|
||||
const upload = multer({
|
||||
storage: multer.memoryStorage(),
|
||||
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) => {
|
||||
console.error(error);
|
||||
res.status(400).json({
|
||||
|
|
@ -99,14 +112,17 @@ async function resolveStylePath(styleId: string): Promise<string> {
|
|||
const direct = path.resolve("styles", styleId);
|
||||
if (await exists(path.join(direct, "theme.json"))) return direct;
|
||||
|
||||
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const repoRoot = path.resolve(moduleDir, "../../..");
|
||||
const fromRepoRoot = path.join(repoRoot, "styles", styleId);
|
||||
const fromRepoRoot = resolveFromRepoRoot("styles", styleId);
|
||||
if (await exists(path.join(fromRepoRoot, "theme.json"))) return fromRepoRoot;
|
||||
|
||||
return direct;
|
||||
}
|
||||
|
||||
function resolveFromRepoRoot(...parts: string[]): string {
|
||||
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
return path.resolve(moduleDir, "../../..", ...parts);
|
||||
}
|
||||
|
||||
async function createDeckArtifacts(options: {
|
||||
source: SourceDocument;
|
||||
instructions?: string;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue