40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import type { Application, Request, Response } from "express";
|
|
import { DateTime } from "luxon";
|
|
/**
|
|
|
|
Registra endpoints de liveness/readiness.
|
|
|
|
/__health: siempre 200 mientras el proceso esté vivo.
|
|
|
|
/__ready : 200 si ready=true, 503 en caso contrario.
|
|
*/
|
|
export function registerHealthRoutes(
|
|
app: Application,
|
|
baseRoutePath: string,
|
|
getStatus: () => { ready: boolean }
|
|
): void {
|
|
// Liveness probe: indica que el proceso responde
|
|
app.get(`${baseRoutePath}/__health`, (_req: Request, res: Response) => {
|
|
// Información mínima y no sensible
|
|
res.status(200).json({
|
|
status: "ok",
|
|
time: DateTime.now().toISO(),
|
|
});
|
|
});
|
|
|
|
// Readiness probe: indica que el servidor está listo para tráfico
|
|
app.get(`${baseRoutePath}/__ready`, (_req: Request, res: Response) => {
|
|
const { ready } = getStatus();
|
|
if (ready) {
|
|
return res.status(200).json({
|
|
status: "ready",
|
|
time: DateTime.now().toISO(),
|
|
});
|
|
}
|
|
return res.status(503).json({
|
|
status: "not_ready",
|
|
time: DateTime.now().toISO(),
|
|
});
|
|
});
|
|
}
|