feat:优化架构

This commit is contained in:
Daniel
2026-03-25 13:43:00 +08:00
parent 8991f2a2d7
commit a2f224d01f
7 changed files with 509 additions and 5 deletions

View File

@@ -30,6 +30,20 @@ app.get("/api/health", (_req, res) => {
res.status(200).json({ ok: true });
});
app.get("/api/tasks/:task_id", (req, res) => {
const taskId = String(req.params.task_id || "").trim();
if (!taskId) return res.status(400).json({ error: "missing task_id" });
const p = path.join(outputsDir, taskId, "task.json");
if (!fs.existsSync(p)) return res.status(404).json({ error: "task not found", task_id: taskId });
try {
const raw = fs.readFileSync(p, "utf8");
const data = JSON.parse(raw);
return res.json(data);
} catch (e) {
return res.status(500).json({ error: "failed to read task", task_id: taskId, detail: String(e) });
}
});
function sseHeaders(res) {
res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
res.setHeader("Cache-Control", "no-cache, no-transform");
@@ -237,6 +251,17 @@ app.post("/api/render", (req, res) => {
for (const line of parts) {
if (!line) continue;
if (line.startsWith("PROG ")) sseSend(res, "prog", line.slice("PROG ".length));
else if (line.startsWith("PROG_SHOT ")) {
const rest = line.slice("PROG_SHOT ".length).trim();
const firstSpace = rest.indexOf(" ");
if (firstSpace > 0) {
const shotId = rest.slice(0, firstSpace).trim();
const status = rest.slice(firstSpace + 1).trim();
sseSend(res, "shot_progress", JSON.stringify({ shot_id: shotId, status }));
} else {
sseSend(res, "line", line);
}
}
else if (line.startsWith("RENDER_DONE ")) sseSend(res, "done", line.slice("RENDER_DONE ".length));
else sseSend(res, "line", line);
}