// Production delivery trial for the Marqdo research workbench design. // This is a real application (Express + JSON persistence), NOT the web Tool: // the design/Preview prototype never runs as a dependency of this server. import express from "express"; import { readFileSync, writeFileSync, existsSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; const here = dirname(fileURLToPath(import.meta.url)); export function createApp({ dataFile }) { const app = express(); app.use(express.json({ limit: "64kb" })); app.use(express.static(join(here, "public"))); const load = () => { if (!existsSync(dataFile)) return { nextId: 1, tasks: [] }; return JSON.parse(readFileSync(dataFile, "utf8")); }; const save = (state) => writeFileSync(dataFile, JSON.stringify(state, null, 2)); const fail = (response, status, code, message) => response.status(status).json({ error: { code, message } }); app.get("/api/tasks", (request, response) => { const state = load(); const query = String(request.query.q ?? "").trim().toLowerCase(); const status = String(request.query.status ?? "").trim(); const tasks = state.tasks.filter((task) => { const matchesQuery = !query || task.title.toLowerCase().includes(query); const matchesStatus = !status || task.status === status; return matchesQuery && matchesStatus; }); response.json({ tasks }); }); app.post("/api/tasks", (request, response) => { const title = typeof request.body?.title === "string" ? request.body.title.trim() : ""; const priority = request.body?.priority ?? "normal"; if (!title) return fail(response, 400, "task.title_required", "任务标题不能为空"); if (title.length > 120) return fail(response, 400, "task.title_too_long", "任务标题不能超过 120 字符"); if (!["low", "normal", "high"].includes(priority)) { return fail(response, 400, "task.priority_invalid", "优先级必须是 low/normal/high"); } const state = load(); const task = { id: state.nextId++, title, priority, status: "open", createdAt: new Date().toISOString(), }; state.tasks.push(task); save(state); response.status(201).json({ task }); }); app.patch("/api/tasks/:id/status", (request, response) => { const id = Number(request.params.id); const status = request.body?.status; if (!Number.isInteger(id) || id < 1) return fail(response, 400, "task.id_invalid", "任务 id 非法"); if (!["open", "doing", "done"].includes(status)) { return fail(response, 400, "task.status_invalid", "状态必须是 open/doing/done"); } const state = load(); const task = state.tasks.find((item) => item.id === id); if (!task) return fail(response, 404, "task.not_found", "任务不存在"); task.status = status; save(state); response.json({ task }); }); app.get("/api/stats", (request, response) => { const state = load(); const total = state.tasks.length; const byStatus = { open: 0, doing: 0, done: 0 }; const byPriority = { low: 0, normal: 0, high: 0 }; for (const task of state.tasks) { byStatus[task.status] += 1; byPriority[task.priority] += 1; } response.json({ total, byStatus, byPriority }); }); app.use((request, response) => fail(response, 404, "app.not_found", `unknown route ${request.method} ${request.path}`), ); return app; } const isMain = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]; if (isMain) { const port = Number(process.env.PORT ?? 4173); const dataFile = process.env.WORKBENCH_DATA ?? join(here, "workbench-data.json"); createApp({ dataFile }).listen(port, "127.0.0.1", () => { console.log(`research-workbench listening on http://127.0.0.1:${port} (data: ${dataFile})`); }); }