From 847c4be26b59da5fc43a21b3714b330c0166120f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Mon, 17 Aug 2026 14:17:24 +0200 Subject: [PATCH] feat: system/http/traefik collectors and docker auto-discovery (M3) Adds the remaining monitor types: a systeminformation-backed system widget (single-flight cached so multiple widgets don't force concurrent samples), an http widget with per-widget consecutive- failure tracking, and a traefik widget that reads router/entrypoint/ middleware status from Traefik's own API. Also adds opt-in label-based docker auto-discovery (discovery.docker in config.yml): containers carrying traefik.enable=true are turned into docker widgets in a synthetic "Discovered" group, re-scanned on a timer and whenever config.yml changes, with manual widgets always taking precedence over a discovered one for the same container. Fixes a real bug surfaced while testing discovery: Next's App Router bundles app/** through its own compiler pass, separate from server.ts (run directly via tsx), so configStore/effectiveConfigStore were silently instantiated twice - one instance watched and updated by server.ts, another frozen instance read by SSR. Live config edits were never reflected on page load without a full process restart. Both stores now key their singleton off globalThis, which both module graphs share within the same process. Verified against a real Traefik v3 container (API-driven router list, ping-based http check) and a labeled nginx container (auto-discovery), including that a config.yml edit now shows up in a fresh page load without restarting the server, in both dev and the production build. --- app/page.tsx | 5 +- components/widgets/http/Widget.tsx | 38 +++++++++++++ components/widgets/registry.ts | 6 +++ components/widgets/system/Widget.tsx | 62 +++++++++++++++++++++ components/widgets/traefik/Widget.tsx | 46 ++++++++++++++++ lib/collectors/http.ts | 18 +++++++ lib/collectors/scheduler.ts | 57 +++++++++++++++++--- lib/collectors/system.ts | 52 ++++++++++++++++++ lib/collectors/traefik.ts | 48 +++++++++++++++++ lib/config/effective.ts | 77 +++++++++++++++++++++++++++ lib/config/loader.ts | 13 ++++- lib/config/schema.ts | 52 ++++++++++++++++++ lib/discovery/traefik-labels.ts | 64 ++++++++++++++++++++++ lib/types/http-result.ts | 7 +++ lib/types/system-result.ts | 9 ++++ lib/types/traefik-result.ts | 19 +++++++ lib/types/widget-result.ts | 7 +++ lib/ws/server.ts | 5 +- server.ts | 7 ++- 19 files changed, 579 insertions(+), 13 deletions(-) create mode 100644 components/widgets/http/Widget.tsx create mode 100644 components/widgets/system/Widget.tsx create mode 100644 components/widgets/traefik/Widget.tsx create mode 100644 lib/collectors/http.ts create mode 100644 lib/collectors/system.ts create mode 100644 lib/collectors/traefik.ts create mode 100644 lib/config/effective.ts create mode 100644 lib/discovery/traefik-labels.ts create mode 100644 lib/types/http-result.ts create mode 100644 lib/types/system-result.ts create mode 100644 lib/types/traefik-result.ts diff --git a/app/page.tsx b/app/page.tsx index 1ae6b2a..0b183ea 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,4 +1,5 @@ -import { configStore, ConfigError } from "@/lib/config/loader"; +import { ConfigError } from "@/lib/config/loader"; +import { effectiveConfigStore } from "@/lib/config/effective"; import { Dashboard } from "@/components/layout/Dashboard"; export const dynamic = "force-dynamic"; @@ -6,7 +7,7 @@ export const dynamic = "force-dynamic"; export default function Home() { let config; try { - config = configStore.get(); + config = effectiveConfigStore.get(); } catch (err) { const message = err instanceof ConfigError ? err.message : "Unknown configuration error"; return ( diff --git a/components/widgets/http/Widget.tsx b/components/widgets/http/Widget.tsx new file mode 100644 index 0000000..139fd28 --- /dev/null +++ b/components/widgets/http/Widget.tsx @@ -0,0 +1,38 @@ +"use client"; + +import type { Widget } from "@/lib/config/schema"; +import { useWidgetSubscription } from "@/lib/ws/client"; + +type HttpWidget = Extract; + +function statusColor(up: boolean | undefined, consecutiveFailures: number): string { + if (up === undefined) return "bg-fg-muted"; + if (up) return "bg-status-up"; + return consecutiveFailures > 2 ? "bg-status-down" : "bg-status-degraded"; +} + +export function HttpWidget({ widget, widgetId }: { widget: HttpWidget; widgetId: string }) { + const result = useWidgetSubscription(widgetId); + const data = result?.type === "http" ? result.data : null; + const errorMessage = result?.type === "error" ? result.message : null; + + return ( +
+
+ {widget.name} + +
+ {errorMessage && {errorMessage}} + {data && ( +
+
Status: {data.statusCode ?? "—"}
+
Latency: {data.latencyMs}ms
+ {!data.up && data.error &&
{data.error}
} +
+ )} +
+ ); +} diff --git a/components/widgets/registry.ts b/components/widgets/registry.ts index a3506a8..8fbbc9c 100644 --- a/components/widgets/registry.ts +++ b/components/widgets/registry.ts @@ -4,6 +4,9 @@ import { BookmarkWidget } from "./bookmark/Widget"; import { SearchWidget } from "./search/Widget"; import { DockerWidget } from "./docker/Widget"; import { DatabaseWidget } from "./database/Widget"; +import { SystemWidget } from "./system/Widget"; +import { HttpWidget } from "./http/Widget"; +import { TraefikWidget } from "./traefik/Widget"; type WidgetComponent = ComponentType<{ widget: Extract; @@ -15,4 +18,7 @@ export const widgetRegistry: { [K in Widget["type"]]: WidgetComponent } = { search: SearchWidget, docker: DockerWidget, database: DatabaseWidget, + system: SystemWidget, + http: HttpWidget, + traefik: TraefikWidget, }; diff --git a/components/widgets/system/Widget.tsx b/components/widgets/system/Widget.tsx new file mode 100644 index 0000000..548d808 --- /dev/null +++ b/components/widgets/system/Widget.tsx @@ -0,0 +1,62 @@ +"use client"; + +import type { Widget } from "@/lib/config/schema"; +import { useWidgetSubscription } from "@/lib/ws/client"; +import { formatBytes } from "@/lib/format"; + +type SystemWidget = Extract; + +function usagePercent(used: number, total: number): number { + return total > 0 ? Math.min(100, (used / total) * 100) : 0; +} + +function UsageBar({ label, percent, detail }: { label: string; percent: number; detail: string }) { + return ( +
+
+ {label} + {detail} +
+
+
+
+
+ ); +} + +export function SystemWidget({ widget, widgetId }: { widget: SystemWidget; widgetId: string }) { + const result = useWidgetSubscription(widgetId); + const data = result?.type === "system" ? result.data : null; + const errorMessage = result?.type === "error" ? result.message : null; + + return ( +
+ {widget.name} + {errorMessage && {errorMessage}} + {data && ( +
+ + + +
+ Network + + ↓ {formatBytes(data.netRxBytesPerSec)}/s · ↑ {formatBytes(data.netTxBytesPerSec)}/s + +
+
+ )} +
+ ); +} diff --git a/components/widgets/traefik/Widget.tsx b/components/widgets/traefik/Widget.tsx new file mode 100644 index 0000000..7d170b2 --- /dev/null +++ b/components/widgets/traefik/Widget.tsx @@ -0,0 +1,46 @@ +"use client"; + +import type { Widget } from "@/lib/config/schema"; +import { useWidgetSubscription } from "@/lib/ws/client"; + +type TraefikWidget = Extract; + +export function TraefikWidget({ widget, widgetId }: { widget: TraefikWidget; widgetId: string }) { + const result = useWidgetSubscription(widgetId); + const data = result?.type === "traefik" ? result.data : null; + const errorMessage = result?.type === "error" ? result.message : null; + + return ( +
+
+ {widget.name} + {data && ( + + {data.entrypoints.map((ep) => ep.address).join(" · ") || `${data.entrypoints.length} entrypoints`} + + )} +
+ {errorMessage && {errorMessage}} + {data && ( +
+ {data.routers.map((router) => ( +
+ + {router.name.replace(/@.*$/, "")} + + + {router.tls && 🔒} + + {router.status} + + +
+ ))} + {data.middlewaresCount} middlewares +
+ )} +
+ ); +} diff --git a/lib/collectors/http.ts b/lib/collectors/http.ts new file mode 100644 index 0000000..2ffb31d --- /dev/null +++ b/lib/collectors/http.ts @@ -0,0 +1,18 @@ +import type { HttpCheckResult } from "@/lib/types/http-result"; + +export async function collectHttp( + url: string, + method: string, + timeoutMs: number, + expectedStatus: number +): Promise> { + const start = performance.now(); + try { + const response = await fetch(url, { method, signal: AbortSignal.timeout(timeoutMs), redirect: "follow" }); + const latencyMs = Math.round(performance.now() - start); + return { up: response.status === expectedStatus, statusCode: response.status, latencyMs, error: null }; + } catch (err) { + const latencyMs = Math.round(performance.now() - start); + return { up: false, statusCode: null, latencyMs, error: (err as Error).message }; + } +} diff --git a/lib/collectors/scheduler.ts b/lib/collectors/scheduler.ts index 1d27c7f..2806b73 100644 --- a/lib/collectors/scheduler.ts +++ b/lib/collectors/scheduler.ts @@ -3,9 +3,13 @@ import type { Config, Widget } from "@/lib/config/schema"; import { flattenWidgets } from "@/lib/config/widgets"; import { parseDuration } from "@/lib/config/duration"; import { collectDockerContainer } from "./docker"; +import { collectSystem } from "./system"; +import { collectHttp } from "./http"; +import { collectTraefik } from "./traefik"; import type { WidgetResult } from "@/lib/types/widget-result"; const dockerLimit = pLimit(4); +const httpLimit = pLimit(8); type ResultListener = (widgetId: string, result: WidgetResult) => void; @@ -13,14 +17,23 @@ interface Job { widget: Widget; intervalMs: number; timer: ReturnType; + consecutiveFailures: number; } +const COLLECTOR_TYPES = new Set(["docker", "database", "system", "http", "traefik"]); + function isCollectorWidget(widget: Widget): boolean { - return widget.type === "docker" || widget.type === "database"; + return COLLECTOR_TYPES.has(widget.type); } function getIntervalMs(widget: Widget): number { - if (widget.type === "docker" || widget.type === "database") { + if ( + widget.type === "docker" || + widget.type === "database" || + widget.type === "system" || + widget.type === "http" || + widget.type === "traefik" + ) { return parseDuration(widget.interval); } return 30_000; @@ -33,6 +46,15 @@ function sameTarget(a: Widget, b: Widget): boolean { if (a.type === "database" && b.type === "database") { return a.containerName === b.containerName; } + if (a.type === "system" && b.type === "system") { + return true; + } + if (a.type === "http" && b.type === "http") { + return a.url === b.url && a.method === b.method && a.expect.status === b.expect.status; + } + if (a.type === "traefik" && b.type === "traefik") { + return a.apiUrl === b.apiUrl; + } return false; } @@ -45,6 +67,20 @@ async function collect(widget: Widget): Promise { const data = await dockerLimit(() => collectDockerContainer(widget.containerName, true)); return { type: "database", data }; } + if (widget.type === "system") { + const data = await collectSystem(); + return { type: "system", data }; + } + if (widget.type === "http") { + const data = await httpLimit(() => + collectHttp(widget.url, widget.method, parseDuration(widget.timeout), widget.expect.status) + ); + return { type: "http", data: { ...data, consecutiveFailures: 0 } }; + } + if (widget.type === "traefik") { + const data = await collectTraefik(widget.apiUrl); + return { type: "traefik", data }; + } throw new Error(`No collector for widget type "${widget.type}"`); } @@ -87,9 +123,9 @@ class CollectorScheduler { if (existing) clearInterval(existing.timer); - const run = () => this.runJob(instance.id, instance.widget); + const run = () => this.runJob(instance.id); const timer = setInterval(run, intervalMs); - this.jobs.set(instance.id, { widget: instance.widget, intervalMs, timer }); + this.jobs.set(instance.id, { widget: instance.widget, intervalMs, timer, consecutiveFailures: 0 }); run(); } @@ -107,13 +143,22 @@ class CollectorScheduler { this.jobs.clear(); } - private async runJob(id: string, widget: Widget): Promise { + private async runJob(id: string): Promise { + const job = this.jobs.get(id); + if (!job) return; + let result: WidgetResult; try { - result = await collect(widget); + result = await collect(job.widget); + if (result.type === "http") { + job.consecutiveFailures = result.data.up ? 0 : job.consecutiveFailures + 1; + result = { type: "http", data: { ...result.data, consecutiveFailures: job.consecutiveFailures } }; + } } catch (err) { result = { type: "error", message: (err as Error).message }; } + + if (!this.jobs.has(id)) return; this.lastResults.set(id, result); for (const listener of this.listeners) listener(id, result); } diff --git a/lib/collectors/system.ts b/lib/collectors/system.ts new file mode 100644 index 0000000..1d3e171 --- /dev/null +++ b/lib/collectors/system.ts @@ -0,0 +1,52 @@ +import si from "systeminformation"; +import type { SystemResult } from "@/lib/types/system-result"; + +const CACHE_MS = 1_000; + +let lastSample: { data: SystemResult; ts: number } | null = null; +let inFlight: Promise | null = null; + +function pickDisk(disks: si.Systeminformation.FsSizeData[]): si.Systeminformation.FsSizeData | undefined { + return disks.find((disk) => disk.mount === "/") ?? disks.sort((a, b) => b.size - a.size)[0]; +} + +async function sample(): Promise { + const [load, mem, disks, net] = await Promise.all([ + si.currentLoad(), + si.mem(), + si.fsSize(), + si.networkStats(), + ]); + + const disk = pickDisk(disks); + const primaryNet = net[0]; + + return { + cpuPercent: load.currentLoad, + memUsedBytes: mem.active, + memTotalBytes: mem.total, + diskUsedBytes: disk?.used ?? 0, + diskTotalBytes: disk?.size ?? 0, + netRxBytesPerSec: primaryNet?.rx_sec ?? 0, + netTxBytesPerSec: primaryNet?.tx_sec ?? 0, + }; +} + +export async function collectSystem(): Promise { + const now = Date.now(); + if (lastSample && now - lastSample.ts < CACHE_MS) { + return lastSample.data; + } + if (inFlight) return inFlight; + + inFlight = sample() + .then((data) => { + lastSample = { data, ts: Date.now() }; + return data; + }) + .finally(() => { + inFlight = null; + }); + + return inFlight; +} diff --git a/lib/collectors/traefik.ts b/lib/collectors/traefik.ts new file mode 100644 index 0000000..02ce651 --- /dev/null +++ b/lib/collectors/traefik.ts @@ -0,0 +1,48 @@ +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, + }; +} diff --git a/lib/config/effective.ts b/lib/config/effective.ts new file mode 100644 index 0000000..cf5fb0b --- /dev/null +++ b/lib/config/effective.ts @@ -0,0 +1,77 @@ +import { configStore } from "./loader"; +import { discoverDockerWidgets } from "@/lib/discovery/traefik-labels"; +import type { Config, Widget } from "./schema"; + +const DISCOVERY_INTERVAL_MS = 60_000; + +type Listener = (config: Config) => void; + +function mergeDiscovered(config: Config, discovered: Widget[]): Config { + if (discovered.length === 0) return config; + return { ...config, groups: [...config.groups, { name: "Discovered", widgets: discovered }] }; +} + +function discoveredKey(widget: Widget): string { + return widget.type === "docker" || widget.type === "database" ? widget.containerName : ""; +} + +function sameDiscoveredSet(a: Widget[], b: Widget[]): boolean { + if (a.length !== b.length) return false; + const setA = new Set(a.map(discoveredKey)); + return b.every((widget) => setA.has(discoveredKey(widget))); +} + +class EffectiveConfigStore { + private discovered: Widget[] = []; + private timer: ReturnType | null = null; + private readonly listeners = new Set(); + + get(): Config { + return mergeDiscovered(configStore.get(), this.discovered); + } + + onUpdate(listener: Listener): () => void { + this.listeners.add(listener); + return () => this.listeners.delete(listener); + } + + async start(): Promise { + configStore.onUpdate(() => { + this.emit(); + void this.scan(); + }); + await this.scan(); + this.timer = setInterval(() => void this.scan(), DISCOVERY_INTERVAL_MS); + } + + stop(): void { + if (this.timer) clearInterval(this.timer); + this.timer = null; + } + + private async scan(): Promise { + try { + const next = await discoverDockerWidgets(configStore.get()); + if (!sameDiscoveredSet(this.discovered, next)) { + this.discovered = next; + this.emit(); + } + } catch (err) { + console.error(`[discovery] ${(err as Error).message}`); + } + } + + private emit(): void { + const effective = this.get(); + for (const listener of this.listeners) listener(effective); + } +} + +// See lib/config/loader.ts for why this is keyed off globalThis rather than +// exported as a plain module singleton. +declare global { + var __pulsenodeEffectiveConfigStore: EffectiveConfigStore | undefined; +} + +export const effectiveConfigStore = globalThis.__pulsenodeEffectiveConfigStore ?? new EffectiveConfigStore(); +globalThis.__pulsenodeEffectiveConfigStore = effectiveConfigStore; diff --git a/lib/config/loader.ts b/lib/config/loader.ts index 0eca951..b995521 100644 --- a/lib/config/loader.ts +++ b/lib/config/loader.ts @@ -106,4 +106,15 @@ class ConfigStore { } } -export const configStore = new ConfigStore(); +// Next.js bundles app/** through its own compiler pass, giving it a module +// registry separate from server.ts (loaded directly via tsx). A plain module +// singleton would end up duplicated - one instance watched by server.ts, +// another frozen instance used by SSR. Both still share the same Node +// process/heap, so keying off globalThis gives every module graph the same +// instance. +declare global { + var __pulsenodeConfigStore: ConfigStore | undefined; +} + +export const configStore = globalThis.__pulsenodeConfigStore ?? new ConfigStore(); +globalThis.__pulsenodeConfigStore = configStore; diff --git a/lib/config/schema.ts b/lib/config/schema.ts index 8644ef2..99fb754 100644 --- a/lib/config/schema.ts +++ b/lib/config/schema.ts @@ -36,11 +36,41 @@ export const databaseWidgetSchema = z.object({ interval: durationSchema.default("10s"), }); +export const systemWidgetSchema = z.object({ + type: z.literal("system"), + name: z.string().default("System"), + interval: durationSchema.default("5s"), +}); + +export const httpWidgetSchema = z.object({ + type: z.literal("http"), + name: z.string(), + url: z.string().url(), + method: z.enum(["GET", "HEAD", "POST"]).default("GET"), + timeout: durationSchema.default("5s"), + interval: durationSchema.default("30s"), + expect: z + .object({ + status: z.number().int().min(100).max(599).default(200), + }) + .default({ status: 200 }), +}); + +export const traefikWidgetSchema = z.object({ + type: z.literal("traefik"), + name: z.string().default("Traefik"), + apiUrl: z.string().url(), + interval: durationSchema.default("15s"), +}); + export const widgetSchema = z.discriminatedUnion("type", [ bookmarkWidgetSchema, searchWidgetSchema, dockerWidgetSchema, databaseWidgetSchema, + systemWidgetSchema, + httpWidgetSchema, + traefikWidgetSchema, ]); export const groupSchema = z.object({ @@ -62,9 +92,31 @@ export const settingsSchema = z }) .default({ title: "PulseNode" }); +const dockerDiscoveryDefaults = { + enabled: false, + socketPath: "/var/run/docker.sock", + labelPrefix: "traefik", + excludeLabels: [] as string[], +}; + +const dockerDiscoverySchema = z.object({ + enabled: z.boolean().default(false), + socketPath: z.string().default("/var/run/docker.sock"), + labelPrefix: z.string().default("traefik"), + network: z.string().optional(), + excludeLabels: z.array(z.string()).default([]), +}); + +export const discoverySchema = z + .object({ + docker: dockerDiscoverySchema.default(dockerDiscoveryDefaults), + }) + .default({ docker: dockerDiscoveryDefaults }); + export const configSchema = z.object({ theme: themeSchema, settings: settingsSchema, + discovery: discoverySchema, groups: z.array(groupSchema).default([]), }); diff --git a/lib/discovery/traefik-labels.ts b/lib/discovery/traefik-labels.ts new file mode 100644 index 0000000..0487912 --- /dev/null +++ b/lib/discovery/traefik-labels.ts @@ -0,0 +1,64 @@ +import Docker from "dockerode"; +import type { Config, Widget } from "@/lib/config/schema"; + +const HOST_RULE_PATTERN = /Host\(`([^`]+)`\)/; + +function extractHostname(labels: Record, prefix: string): string | null { + for (const [key, value] of Object.entries(labels)) { + if (key.startsWith(`${prefix}.http.routers.`) && key.endsWith(".rule")) { + const match = HOST_RULE_PATTERN.exec(value); + if (match) return match[1]; + } + } + return null; +} + +export async function discoverDockerWidgets(config: Config): Promise { + const settings = config.discovery.docker; + if (!settings.enabled) return []; + + const docker = new Docker({ socketPath: settings.socketPath }); + const containers = await docker.listContainers({ all: false }); + + const manualContainerNames = new Set( + config.groups.flatMap((group) => + group.widgets + .filter( + (widget): widget is Extract => + widget.type === "docker" || widget.type === "database" + ) + .map((widget) => widget.containerName) + ) + ); + + const prefix = settings.labelPrefix; + const discovered: Widget[] = []; + + for (const container of containers) { + const labels = container.Labels ?? {}; + + if (labels[`${prefix}.enable`] !== "true") continue; + if (settings.excludeLabels.some((label) => labels[label] !== undefined)) continue; + + const name = container.Names[0]?.replace(/^\//, ""); + if (!name || manualContainerNames.has(name)) continue; + + if (settings.network) { + const containerNetwork = labels[`${prefix}.docker.network`]; + if (containerNetwork && containerNetwork !== settings.network) continue; + } + + const hostname = extractHostname(labels, prefix); + + discovered.push({ + type: "docker", + name, + containerName: name, + href: hostname ? `https://${hostname}` : undefined, + showStats: true, + interval: "10s", + }); + } + + return discovered; +} diff --git a/lib/types/http-result.ts b/lib/types/http-result.ts new file mode 100644 index 0000000..681bc34 --- /dev/null +++ b/lib/types/http-result.ts @@ -0,0 +1,7 @@ +export interface HttpCheckResult { + up: boolean; + statusCode: number | null; + latencyMs: number; + consecutiveFailures: number; + error: string | null; +} diff --git a/lib/types/system-result.ts b/lib/types/system-result.ts new file mode 100644 index 0000000..304751c --- /dev/null +++ b/lib/types/system-result.ts @@ -0,0 +1,9 @@ +export interface SystemResult { + cpuPercent: number; + memUsedBytes: number; + memTotalBytes: number; + diskUsedBytes: number; + diskTotalBytes: number; + netRxBytesPerSec: number; + netTxBytesPerSec: number; +} diff --git a/lib/types/traefik-result.ts b/lib/types/traefik-result.ts new file mode 100644 index 0000000..546752c --- /dev/null +++ b/lib/types/traefik-result.ts @@ -0,0 +1,19 @@ +export interface TraefikRouterInfo { + name: string; + rule: string; + service: string; + status: string; + tls: boolean; + entryPoints: string[]; +} + +export interface TraefikEntrypointInfo { + name: string; + address: string; +} + +export interface TraefikResult { + routers: TraefikRouterInfo[]; + entrypoints: TraefikEntrypointInfo[]; + middlewaresCount: number; +} diff --git a/lib/types/widget-result.ts b/lib/types/widget-result.ts index a1d70fb..dbcd09c 100644 --- a/lib/types/widget-result.ts +++ b/lib/types/widget-result.ts @@ -1,3 +1,7 @@ +import type { SystemResult } from "./system-result"; +import type { HttpCheckResult } from "./http-result"; +import type { TraefikResult } from "./traefik-result"; + export interface DockerContainerResult { status: "running" | "exited" | "restarting" | "paused" | "dead" | "created" | "unknown"; health: "healthy" | "unhealthy" | "starting" | "none"; @@ -13,4 +17,7 @@ export interface DockerContainerResult { export type WidgetResult = | { type: "docker"; data: DockerContainerResult } | { type: "database"; data: DockerContainerResult } + | { type: "system"; data: SystemResult } + | { type: "http"; data: HttpCheckResult } + | { type: "traefik"; data: TraefikResult } | { type: "error"; message: string }; diff --git a/lib/ws/server.ts b/lib/ws/server.ts index 6590a88..2f5ae8f 100644 --- a/lib/ws/server.ts +++ b/lib/ws/server.ts @@ -2,6 +2,7 @@ import type { Server as HttpServer } from "node:http"; import { WebSocket, WebSocketServer } from "ws"; import { collectorScheduler } from "@/lib/collectors/scheduler"; import { configStore, type ConfigError } from "@/lib/config/loader"; +import { effectiveConfigStore } from "@/lib/config/effective"; import type { Config } from "@/lib/config/schema"; import type { WidgetResult } from "@/lib/types/widget-result"; @@ -47,7 +48,7 @@ export function attachWebSocketServer(httpServer: HttpServer): WebSocketServer { allSockets.add(socket); try { - const config = configStore.get(); + const config = effectiveConfigStore.get(); send(socket, { topic: "config", type: "config:update", ts: Date.now(), data: config }); } catch { // no valid config loaded yet; client keeps its SSR-seeded config until one arrives @@ -78,7 +79,7 @@ export function attachWebSocketServer(httpServer: HttpServer): WebSocketServer { for (const socket of set) send(socket, envelope); }); - configStore.onUpdate((config) => { + effectiveConfigStore.onUpdate((config) => { const envelope: Envelope = { topic: "config", type: "config:update", ts: Date.now(), data: config }; for (const socket of allSockets) send(socket, envelope); }); diff --git a/server.ts b/server.ts index 1971dec..0e44d0e 100644 --- a/server.ts +++ b/server.ts @@ -2,6 +2,7 @@ import { createServer } from "node:http"; import next from "next"; import { attachWebSocketServer } from "./lib/ws/server"; import { configStore } from "./lib/config/loader"; +import { effectiveConfigStore } from "./lib/config/effective"; import { collectorScheduler } from "./lib/collectors/scheduler"; const port = Number(process.env.PORT ?? 3000); @@ -17,8 +18,9 @@ async function main(): Promise { configStore.load(); configStore.watch(); - collectorScheduler.start(configStore.get()); - configStore.onUpdate((config) => collectorScheduler.reconcile(config)); + await effectiveConfigStore.start(); + collectorScheduler.start(effectiveConfigStore.get()); + effectiveConfigStore.onUpdate((config) => collectorScheduler.reconcile(config)); const httpServer = createServer((req, res) => { handle(req, res); @@ -39,6 +41,7 @@ async function main(): Promise { function shutdown(): void { collectorScheduler.stop(); + effectiveConfigStore.stop(); configStore.stop(); httpServer.close(() => process.exit(0)); setTimeout(() => process.exit(0), 5_000).unref();