paperclip/server/src/app.ts
Forgotten c9d7cbfe44 Add API server with routes, services, and middleware
Express server with CRUD routes for agents, goals, issues, projects,
and activity log. Includes validation middleware, structured error
handling, request logging, and health check endpoint with tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 13:31:58 -06:00

42 lines
1.3 KiB
TypeScript

import express, { Router } from "express";
import path from "node:path";
import { fileURLToPath } from "node:url";
import type { Db } from "@paperclip/db";
import { httpLogger, errorHandler } from "./middleware/index.js";
import { healthRoutes } from "./routes/health.js";
import { agentRoutes } from "./routes/agents.js";
import { projectRoutes } from "./routes/projects.js";
import { issueRoutes } from "./routes/issues.js";
import { goalRoutes } from "./routes/goals.js";
import { activityRoutes } from "./routes/activity.js";
export function createApp(db: Db, opts: { serveUi: boolean }) {
const app = express();
app.use(express.json());
app.use(httpLogger);
// Mount API routes
const api = Router();
api.use("/health", healthRoutes());
api.use("/agents", agentRoutes(db));
api.use("/projects", projectRoutes(db));
api.use("/issues", issueRoutes(db));
api.use("/goals", goalRoutes(db));
api.use("/activity", activityRoutes(db));
app.use("/api", api);
// SPA fallback for serving the UI build
if (opts.serveUi) {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const uiDist = path.resolve(__dirname, "../../ui/dist");
app.use(express.static(uiDist));
app.get("*", (_req, res) => {
res.sendFile(path.join(uiDist, "index.html"));
});
}
app.use(errorHandler);
return app;
}