4efa71515e
Data layer: - Fuzzy scorer (packages/core/src/fuzzy.ts) with index recovery + unit tests - Backlink computation (packages/core/src/backlinks.ts) incl. entity-level backlinks - Consistency checker (packages/core/src/consistency.ts) incl. reciprocity/scope + unit tests - StorageAdapter contract extended with listEntities/searchEntities/saveEntity/deleteEntity; implemented in Local (Dexie) and Server adapters; contract tests cover entity CRUD + ?q= search - WorldItem.fields now supports list values (keyPeople, rivals); expanded RelationshipKind - @mention autocomplete extension + picker in the editor package Entity UI (apps/web/src/features/entities/): - Generic field renderer (EntityFields) driven by ENTITY_FIELDS/FieldDef - Tabbed directory with fuzzy search + entity cards (EntitiesView) - Create/edit/delete drawer (EntityEditor) reusing shared fields - Relationship editor + force-directed SVG relationship map - Entity engine stylesheet; wired into nav Store: project-store entity methods (load/create/update/delete) over the StorageAdapter contract. CI-green: typecheck, lint, 65 tests, and web build all pass.
91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
import { fileURLToPath } from "node:url";
|
|
import Fastify from "fastify";
|
|
import cors from "@fastify/cors";
|
|
import cookie from "@fastify/cookie";
|
|
import websocket from "@fastify/websocket";
|
|
import { ZodError } from "zod";
|
|
import { config } from "./config.js";
|
|
import { openDb, ensureAdmin } from "./db.js";
|
|
import { hashPassword, registerAuthHook, registerAuthRoutes } from "./auth.js";
|
|
import { registerRoutes } from "./routes.js";
|
|
import { registerWs } from "./ws.js";
|
|
|
|
/** A Fastify error carrying a status code and a serializable payload. */
|
|
export interface HttpError extends Error {
|
|
statusCode: number;
|
|
payload: object;
|
|
}
|
|
|
|
declare module "fastify" {
|
|
interface FastifyInstance {
|
|
/** Create a throwable HTTP error: `throw app.httpErrors.create(404, { error: "not_found" })`. */
|
|
httpErrors: { create(status: number, payload: object): HttpError };
|
|
}
|
|
}
|
|
|
|
function makeHttpError(status: number, payload: object): HttpError {
|
|
const e = new Error("http_error") as HttpError;
|
|
e.statusCode = status;
|
|
e.payload = payload;
|
|
return e;
|
|
}
|
|
|
|
export async function buildApp() {
|
|
const app = Fastify({ logger: true });
|
|
await app.register(cors, { origin: true, credentials: true });
|
|
await app.register(cookie);
|
|
await app.register(websocket);
|
|
|
|
const db = openDb();
|
|
app.decorate("db", db);
|
|
app.decorate("httpErrors", { create: makeHttpError });
|
|
await ensureAdmin(db, hashPassword);
|
|
|
|
// Serialize thrown errors into the API's `{ error: "code" }` shape.
|
|
app.setErrorHandler((err, _req, reply) => {
|
|
if (err instanceof ZodError) {
|
|
return reply.code(400).send({ error: "invalid_input", issues: err.issues });
|
|
}
|
|
const status = (err as HttpError).statusCode ?? 500;
|
|
const payload = (err as HttpError).payload ?? { error: "internal_error" };
|
|
return reply.code(status).send(payload);
|
|
});
|
|
|
|
app.get("/api/health", async () => {
|
|
let dbUp = false;
|
|
try {
|
|
dbUp = db.prepare("SELECT 1").get() != null;
|
|
} catch {
|
|
dbUp = false;
|
|
}
|
|
return { ok: dbUp, db: dbUp ? "up" : "down", version: config.version, uptime: process.uptime() };
|
|
});
|
|
|
|
registerAuthHook(app);
|
|
registerAuthRoutes(app);
|
|
registerRoutes(app);
|
|
registerWs(app);
|
|
|
|
return app;
|
|
}
|
|
|
|
export async function start() {
|
|
const app = await buildApp();
|
|
await app.listen({ port: config.port, host: "0.0.0.0" });
|
|
return app;
|
|
}
|
|
|
|
// Auto-boot only when this file is the CLI entry point (pnpm dev / start),
|
|
// not when imported by tests (vitest) — otherwise start() would try to bind
|
|
// the port and tear the test process down.
|
|
const invokedDirectly =
|
|
process.argv[1] !== undefined &&
|
|
fileURLToPath(import.meta.url) === process.argv[1];
|
|
|
|
if (invokedDirectly) {
|
|
start().catch((err) => {
|
|
console.error("Failed to start server:", err);
|
|
process.exit(1);
|
|
});
|
|
}
|