import { createServer, type Server } from "node:http"; import type { Logger } from "pino"; import type { HealthSnapshot } from "./processor.js"; /** GET /health (or /) → 200 {rooms, cursor, pending, lastProcessedAt}; 503 when the DB count fails. */ export function startHealthServer(port: number, snapshot: () => Promise, logger: Logger): Server { const server = createServer((req, res) => { if (req.method !== "GET" || (req.url !== "/" && req.url !== "/health")) { res.writeHead(404).end(); return; } snapshot().then( (body) => { res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify(body)); }, (err: unknown) => { logger.warn({ err: err instanceof Error ? err.message : String(err) }, "health: snapshot failed"); res.writeHead(503, { "content-type": "application/json" }).end(JSON.stringify({ status: "down" })); }, ); }); server.listen(port, () => logger.info({ port }, "health: listening")); return server; }