Adds a custom server.ts (http server + Next request handler + a noServer:true WebSocket server on /ws) so the app can push live data without polling. A CollectorScheduler runs one interval-based job per docker/database widget instance, backed by dockerode against /var/run/docker.sock, and reconciles jobs when config.yml changes. Config hot-reload is now fully wired: chokidar watches config.yml/.env, re-validates on change, and broadcasts config:update (or a non-destructive config:error, keeping the last-good config) to every connected browser. The client subscribes to per-widget topics over a single shared WebSocket with exponential-backoff reconnect and last-result caching for instant resubscribe. Verified end-to-end against real throwaway containers (nginx, redis): live CPU/mem/uptime/health streamed over the socket, interval changes picked up without a server restart, and a broken config.yml correctly falls back to the last-valid config instead of crashing the app.
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import { z } from "zod";
|
|
|
|
const searchEngineSchema = z.enum(["duckduckgo", "google", "bing"]);
|
|
|
|
const durationSchema = z
|
|
.string()
|
|
.regex(/^\d+(ms|s|m|h)$/, 'Expected a duration like "500ms", "5s", "1m", or "1h"');
|
|
|
|
export const bookmarkWidgetSchema = z.object({
|
|
type: z.literal("bookmark"),
|
|
name: z.string(),
|
|
href: z.string().url(),
|
|
description: z.string().optional(),
|
|
});
|
|
|
|
export const searchWidgetSchema = z.object({
|
|
type: z.literal("search"),
|
|
engines: z.array(searchEngineSchema).min(1).default(["duckduckgo"]),
|
|
defaultEngine: searchEngineSchema.optional(),
|
|
});
|
|
|
|
export const dockerWidgetSchema = z.object({
|
|
type: z.literal("docker"),
|
|
name: z.string(),
|
|
containerName: z.string(),
|
|
href: z.string().url().optional(),
|
|
showStats: z.boolean().default(true),
|
|
interval: durationSchema.default("5s"),
|
|
});
|
|
|
|
export const databaseWidgetSchema = z.object({
|
|
type: z.literal("database"),
|
|
name: z.string(),
|
|
containerName: z.string(),
|
|
engine: z.enum(["postgres", "redis"]),
|
|
interval: durationSchema.default("10s"),
|
|
});
|
|
|
|
export const widgetSchema = z.discriminatedUnion("type", [
|
|
bookmarkWidgetSchema,
|
|
searchWidgetSchema,
|
|
dockerWidgetSchema,
|
|
databaseWidgetSchema,
|
|
]);
|
|
|
|
export const groupSchema = z.object({
|
|
name: z.string(),
|
|
widgets: z.array(widgetSchema),
|
|
});
|
|
|
|
export const themeSchema = z
|
|
.object({
|
|
mode: z.enum(["dark", "light", "auto"]).default("dark"),
|
|
variables: z.record(z.string(), z.string()).default({}),
|
|
customCssPath: z.string().optional(),
|
|
})
|
|
.default({ mode: "dark", variables: {} });
|
|
|
|
export const settingsSchema = z
|
|
.object({
|
|
title: z.string().default("PulseNode"),
|
|
})
|
|
.default({ title: "PulseNode" });
|
|
|
|
export const configSchema = z.object({
|
|
theme: themeSchema,
|
|
settings: settingsSchema,
|
|
groups: z.array(groupSchema).default([]),
|
|
});
|
|
|
|
export type Config = z.infer<typeof configSchema>;
|
|
export type Widget = z.infer<typeof widgetSchema>;
|
|
export type Group = z.infer<typeof groupSchema>;
|
|
export type SearchEngine = z.infer<typeof searchEngineSchema>;
|