import { Controller, Get, Res, Inject } from "@nestjs/common"; import type { Response } from "express"; import { HealthService, type ComponentHealth } from "./health.service"; interface ReadinessBody { status: "ready" | "degraded"; components: Record; } @Controller() export class HealthController { constructor(@Inject(HealthService) private readonly health: HealthService) {} @Get("healthz") liveness(@Res() res: Response): void { res.status(200).json({ status: "alive" }); } @Get("readyz") async readiness(@Res() res: Response): Promise { const components = await this.health.components(); const ready = Object.values(components).every((c) => c.status === "up"); const body: ReadinessBody = { status: ready ? "ready" : "degraded", components }; res.status(ready ? 200 : 503).json(body); } }