Files
cloudflare-domain-manager/apps/api/src/lib/cloudflare/healthcheck-service.ts
T
Denozordec 3f6f402872
quality / commitlint (push) Skipped
CD / update-wiki (push) Failing after 8s
quality / changes (push) Successful in 5s
quality / docker-check (push) Skipped
quality / web (push) Successful in 50s
quality / api (push) Successful in 41s
CD / quality (push) Successful in 1m40s
CD / publish (push) Successful in 1m33s
feat(health-checks): enhance health check functionality and add new routes
- Introduced origin health check routes and integrated them into the application.
- Updated health check configuration to include success recovery thresholds.
- Expanded error handling with new error codes for health check failures.
- Added new service routes for managing health checks, including creation and listing.
- Improved health check service logic to track consecutive successes and failures.

This commit enhances the health check capabilities, providing better monitoring and management of service health.
2026-08-19 12:26:12 +07:00

111 lines
3.1 KiB
TypeScript

import type { CfHealthCheck } from "@cfdm/shared";
import { CF_API_BASE, handleCfResponse } from "./http.js";
export interface CfHealthCheckHttpConfig {
method?: string;
path?: string;
expected_codes?: string[];
header?: Record<string, string[]>;
port?: number;
follow_redirects?: boolean;
allow_insecure?: boolean;
}
export interface CfHealthCheckTcpConfig {
method?: "connection_established";
port?: number;
}
export interface CfHealthCheckPayload {
address: string;
name: string;
type?: "HTTP" | "HTTPS" | "TCP";
description?: string;
interval?: number;
timeout?: number;
retries?: number;
consecutive_fails?: number;
consecutive_successes?: number;
suspended?: boolean;
check_regions?: string[];
http_config?: CfHealthCheckHttpConfig;
tcp_config?: CfHealthCheckTcpConfig;
}
export function createHealthCheckAdapter(token: string) {
return {
async listHealthChecks(zoneId: string): Promise<CfHealthCheck[]> {
const response = await fetch(
`${CF_API_BASE}/zones/${zoneId}/healthchecks`,
{
headers: { Authorization: `Bearer ${token}` },
signal: AbortSignal.timeout(30_000),
},
);
return handleCfResponse(response, "list_healthchecks");
},
async getHealthCheck(zoneId: string, id: string): Promise<CfHealthCheck> {
const response = await fetch(
`${CF_API_BASE}/zones/${zoneId}/healthchecks/${id}`,
{
headers: { Authorization: `Bearer ${token}` },
signal: AbortSignal.timeout(30_000),
},
);
return handleCfResponse(response, "get_healthcheck");
},
async createHealthCheck(
zoneId: string,
payload: CfHealthCheckPayload,
): Promise<CfHealthCheck> {
const response = await fetch(
`${CF_API_BASE}/zones/${zoneId}/healthchecks`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
signal: AbortSignal.timeout(30_000),
},
);
return handleCfResponse(response, "create_healthcheck");
},
async updateHealthCheck(
zoneId: string,
id: string,
payload: CfHealthCheckPayload,
): Promise<CfHealthCheck> {
const response = await fetch(
`${CF_API_BASE}/zones/${zoneId}/healthchecks/${id}`,
{
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
signal: AbortSignal.timeout(30_000),
},
);
return handleCfResponse(response, "update_healthcheck");
},
async deleteHealthCheck(zoneId: string, id: string): Promise<void> {
const response = await fetch(
`${CF_API_BASE}/zones/${zoneId}/healthchecks/${id}`,
{
method: "DELETE",
headers: { Authorization: `Bearer ${token}` },
signal: AbortSignal.timeout(30_000),
},
);
await handleCfResponse(response, "delete_healthcheck");
},
};
}