Files
Nexus/apps/web/src/app/api/health/route.ts
T
Hartmut b41c1d2501
CI / Architecture Guardrails (push) Successful in 2m38s
CI / Assistant Split Regression (push) Successful in 3m33s
CI / Typecheck (push) Successful in 3m51s
CI / Lint (push) Successful in 5m2s
CI / E2E Tests (push) Has been cancelled
CI / Fresh-Linux Docker Deploy (push) Has been cancelled
CI / Release Images (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
rename(phase 1): CapaKraken → Nexus across code, UI, docs, CI (#61)
rename(phase 1): CapaKraken → Nexus across code, UI, docs, CI (#61)

Co-authored-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
Co-committed-by: Hartmut Nörenberg <hn@hartmut-noerenberg.com>
2026-05-21 16:28:40 +02:00

55 lines
1.4 KiB
TypeScript

import { NextResponse } from "next/server";
import { prisma } from "@nexus/db";
import { createConnection } from "net";
export const dynamic = "force-dynamic";
export const runtime = "nodejs";
const REDIS_URL = process.env["REDIS_URL"] ?? "redis://localhost:6380";
async function checkDb(): Promise<"ok" | "error"> {
try {
await prisma.$queryRaw`SELECT 1`;
return "ok";
} catch {
return "error";
}
}
async function checkRedis(): Promise<"ok" | "error"> {
return new Promise((resolve) => {
try {
const url = new URL(REDIS_URL);
const host = url.hostname || "localhost";
const port = parseInt(url.port || "6379", 10);
const socket = createConnection({ host, port }, () => {
socket.write("*1\r\n$4\r\nPING\r\n");
});
socket.setTimeout(2000);
socket.on("data", (data) => {
socket.destroy();
resolve(data.toString().includes("PONG") ? "ok" : "error");
});
socket.on("timeout", () => {
socket.destroy();
resolve("error");
});
socket.on("error", () => {
socket.destroy();
resolve("error");
});
} catch {
resolve("error");
}
});
}
export async function GET() {
const [db, redis] = await Promise.all([checkDb(), checkRedis()]);
const ok = db === "ok" && redis === "ok";
return NextResponse.json(
{ status: ok ? "ok" : "degraded", db, redis },
{ status: ok ? 200 : 503 },
);
}