2026-08-17 20:45:29 +02:00
|
|
|
import type { CoolifyServiceWidget } from "@/lib/config/schema";
|
|
|
|
|
import type { ServiceStat } from "@/lib/types/service-result";
|
|
|
|
|
import { serviceBaseUrl } from "./base-url";
|
|
|
|
|
|
|
|
|
|
const DETAIL_LIMIT = 5;
|
|
|
|
|
|
|
|
|
|
interface CoolifyProject {
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 21:11:56 +02:00
|
|
|
async function fetchCoolify<T>(base: string, path: string, apiToken: string): Promise<T> {
|
|
|
|
|
const response = await fetch(`${base}${path}`, {
|
|
|
|
|
headers: { Authorization: `Bearer ${apiToken}` },
|
2026-08-17 20:45:29 +02:00
|
|
|
signal: AbortSignal.timeout(5_000),
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Coolify API returned ${response.status}`);
|
|
|
|
|
}
|
2026-08-17 21:11:56 +02:00
|
|
|
return (await response.json()) as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function collectCoolify(
|
|
|
|
|
widget: CoolifyServiceWidget
|
|
|
|
|
): Promise<{ stats: ServiceStat[]; detail?: string[] }> {
|
|
|
|
|
const base = serviceBaseUrl(widget);
|
|
|
|
|
const [projects, resources] = await Promise.all([
|
|
|
|
|
fetchCoolify<CoolifyProject[]>(base, "/api/v1/projects", widget.apiToken),
|
|
|
|
|
fetchCoolify<unknown[]>(base, "/api/v1/resources", widget.apiToken),
|
|
|
|
|
]);
|
2026-08-17 20:45:29 +02:00
|
|
|
const names = projects.slice(0, DETAIL_LIMIT).map((p) => p.name);
|
|
|
|
|
if (projects.length > DETAIL_LIMIT) names.push(`+${projects.length - DETAIL_LIMIT} more`);
|
|
|
|
|
|
|
|
|
|
return {
|
2026-08-17 21:11:56 +02:00
|
|
|
stats: [
|
|
|
|
|
{ label: "Projects", value: String(projects.length) },
|
|
|
|
|
{ label: "Resources", value: String(resources.length) },
|
|
|
|
|
],
|
2026-08-17 20:45:29 +02:00
|
|
|
detail: names.length > 0 ? names : undefined,
|
|
|
|
|
};
|
|
|
|
|
}
|