Files

44 lines
1.8 KiB
TypeScript

import { NextResponse } from "next/server";
import { checkAllDependencies, hasDegradedDependency } from "@/lib/health-checks";
// GET /api/health — liveness (docs/04, BACKLOG P0-3).
//
// Fixed finding: this used to hardcode `ok: true` and only ever check
// *presence* of DATABASE_URL/REDIS_URL, never whether anything was actually
// reachable -- "an uptime monitor watching HTTP 200 would show green for a
// dying instance" (docs/architecture/ARCHITECTURE.md). Each dependency below
// now reports a real state: "ok" (live round trip succeeded), "error"
// (configured but unreachable -- see `detail`), "not_configured", or
// "not_integrated" (env var present but no client code reads it -- honest
// about `queue`, which has no wired Redis client in this repo).
//
// This route stays a LIVENESS check: it always answers 200 as long as the
// process itself can run the handler (that's what "liveness" means -- an
// external dependency outage is not a reason to restart this instance). The
// body's `ok` field is the honest signal instead: false whenever a
// dependency that IS configured is failing. For a readiness gate that DOES
// fail closed on missing/broken dependencies, see GET /api/ready.
export async function GET() {
const deps = await checkAllDependencies();
const degraded = hasDegradedDependency(deps);
return NextResponse.json({
ok: !degraded,
service: "0pt-web",
db: deps.db.status,
queue: deps.queue.status,
engine: deps.engine.status,
stripe: deps.stripe.status,
...(degraded
? {
detail: Object.fromEntries(
Object.entries(deps)
.filter(([, v]) => v.status === "error")
.map(([k, v]) => [k, v.detail]),
),
}
: {}),
ts: new Date().toISOString(),
});
}