49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import type { TraefikResult } from "@/lib/types/traefik-result";
|
|||
|
|
|
||
|
|
interface RawRouter {
|
||
|
|
name: string;
|
||
|
|
rule: string;
|
||
|
|
service: string;
|
||
|
|
status: string;
|
||
|
|
tls?: unknown;
|
||
|
|
entryPoints?: string[];
|
||
|
|
}
|
||
|
|
|
||
|
|
interface RawEntrypoint {
|
||
|
|
name: string;
|
||
|
|
address: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
async function fetchJson<T>(url: string): Promise<T | null> {
|
||
|
|
const response = await fetch(url, { signal: AbortSignal.timeout(5_000) });
|
||
|
|
if (!response.ok) return null;
|
||
|
|
return (await response.json()) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function collectTraefik(apiUrl: string): Promise<TraefikResult> {
|
||
|
|
const base = apiUrl.replace(/\/$/, "");
|
||
|
|
|
||
|
|
const [routers, entrypoints, middlewares] = await Promise.all([
|
||
|
|
fetchJson<RawRouter[]>(`${base}/http/routers`),
|
||
|
|
fetchJson<RawEntrypoint[]>(`${base}/entrypoints`),
|
||
|
|
fetchJson<unknown[]>(`${base}/http/middlewares`),
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (routers === null) {
|
||
|
|
throw new Error(`Traefik API at ${base}/http/routers is unreachable or returned an error`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
routers: routers.map((router) => ({
|
||
|
|
name: router.name,
|
||
|
|
rule: router.rule,
|
||
|
|
service: router.service,
|
||
|
|
status: router.status,
|
||
|
|
tls: Boolean(router.tls),
|
||
|
|
entryPoints: router.entryPoints ?? [],
|
||
|
|
})),
|
||
|
|
entrypoints: (entrypoints ?? []).map((ep) => ({ name: ep.name, address: ep.address })),
|
||
|
|
middlewaresCount: middlewares?.length ?? 0,
|
||
|
|
};
|
||
|
|
}
|