Files
boutmoun123 1b753a419a
API Gateway CI / Build, lint, and test (push) Successful in 13m33s
API Gateway CI / Build API Gateway image (push) Successful in 5m43s
feat: sync marketplace dashboard and staging CI
2026-11-02 17:28:58 +03:00

61 lines
1.6 KiB
TypeScript

import { NextResponse } from "next/server";
const baseUrls = Array.from(
new Set(
[
process.env.API_BASE_URL,
process.env.NEXT_PUBLIC_API_BASE_URL,
...(process.env.API_FALLBACK_URLS ?? "").split(","),
]
.map((value) => value?.trim().replace(/\/$/, ""))
.filter((value): value is string => Boolean(value)),
),
);
export const dynamic = "force-dynamic";
export async function GET() {
const checks = await Promise.all(
baseUrls.map(async (baseUrl, index) => {
const startedAt = Date.now();
try {
const response = await fetch(`${baseUrl}/health`, {
cache: "no-store",
signal: AbortSignal.timeout(4_000),
});
return {
upstream: baseUrl,
primary: index === 0,
reachable: response.ok,
status: response.status,
latencyMs: Date.now() - startedAt,
};
} catch (error) {
return {
upstream: baseUrl,
primary: index === 0,
reachable: false,
status: null,
latencyMs: Date.now() - startedAt,
error: error instanceof Error ? error.message : String(error),
};
}
}),
);
const active = checks.find((check) => check.reachable) ?? null;
return NextResponse.json(
{
state: active ? (active.primary ? "online" : "degraded") : "offline",
activeUpstream: active?.upstream ?? null,
latencyMs: active?.latencyMs ?? null,
checks,
checkedAt: new Date().toISOString(),
},
{
status: active ? 200 : 503,
headers: { "cache-control": "no-store" },
},
);
}