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(url: string): Promise { 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 { const base = apiUrl.replace(/\/$/, ""); const [routers, entrypoints, middlewares] = await Promise.all([ fetchJson(`${base}/http/routers`), fetchJson(`${base}/entrypoints`), fetchJson(`${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, }; }