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";
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 16:58:47 +02:00
|
|
|
export async function collectCoolify(widget: CoolifyServiceWidget): Promise<{ stats: ServiceStat[] }> {
|
2026-08-17 21:11:56 +02:00
|
|
|
const base = serviceBaseUrl(widget);
|
|
|
|
|
const [projects, resources] = await Promise.all([
|
2026-08-18 16:58:47 +02:00
|
|
|
fetchCoolify<unknown[]>(base, "/api/v1/projects", widget.apiToken),
|
2026-08-17 21:11:56 +02:00
|
|
|
fetchCoolify<unknown[]>(base, "/api/v1/resources", widget.apiToken),
|
|
|
|
|
]);
|
2026-08-17 20:45:29 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
}
|