mirror of
https://github.com/fosrl/pangolin.git
synced 2026-04-18 15:56:35 +00:00
Create hcs freely
This commit is contained in:
@@ -427,13 +427,6 @@ authenticated.get(
|
||||
resource.listResources
|
||||
);
|
||||
|
||||
authenticated.get(
|
||||
"/org/:orgId/health-checks",
|
||||
verifyOrgAccess,
|
||||
verifyUserHasAction(ActionsEnum.listHealthChecks),
|
||||
resource.listHealthChecks
|
||||
);
|
||||
|
||||
authenticated.get(
|
||||
"/org/:orgId/resource-names",
|
||||
verifyOrgAccess,
|
||||
|
||||
28
server/routers/healthChecks/types.ts
Normal file
28
server/routers/healthChecks/types.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
export type ListHealthChecksResponse = {
|
||||
healthChecks: {
|
||||
targetHealthCheckId: number;
|
||||
name: string;
|
||||
hcEnabled: boolean;
|
||||
hcHealth: "unknown" | "healthy" | "unhealthy";
|
||||
hcMode: string | null;
|
||||
hcHostname: string | null;
|
||||
hcPort: number | null;
|
||||
hcPath: string | null;
|
||||
hcScheme: string | null;
|
||||
hcMethod: string | null;
|
||||
hcInterval: number | null;
|
||||
hcUnhealthyInterval: number | null;
|
||||
hcTimeout: number | null;
|
||||
hcHeaders: string | null;
|
||||
hcFollowRedirects: boolean | null;
|
||||
hcStatus: number | null;
|
||||
hcTlsServerName: string | null;
|
||||
hcHealthyThreshold: number | null;
|
||||
hcUnhealthyThreshold: number | null;
|
||||
}[];
|
||||
pagination: {
|
||||
total: number;
|
||||
limit: number;
|
||||
offset: number;
|
||||
};
|
||||
};
|
||||
@@ -32,4 +32,3 @@ export * from "./addUserToResource";
|
||||
export * from "./removeUserFromResource";
|
||||
export * from "./listAllResourceNames";
|
||||
export * from "./removeEmailFromResourceWhitelist";
|
||||
export * from "./listHealthChecks";
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
import { db, targetHealthCheck, targets, resources } from "@server/db";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
import { eq, sql, inArray } from "drizzle-orm";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { z } from "zod";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
const listHealthChecksParamsSchema = z.strictObject({
|
||||
orgId: z.string().nonempty()
|
||||
});
|
||||
|
||||
const listHealthChecksSchema = z.object({
|
||||
limit: z
|
||||
.string()
|
||||
.optional()
|
||||
.default("1000")
|
||||
.transform(Number)
|
||||
.pipe(z.int().positive()),
|
||||
offset: z
|
||||
.string()
|
||||
.optional()
|
||||
.default("0")
|
||||
.transform(Number)
|
||||
.pipe(z.int().nonnegative())
|
||||
});
|
||||
|
||||
export type ListHealthChecksResponse = {
|
||||
healthChecks: {
|
||||
targetHealthCheckId: number;
|
||||
resourceId: number;
|
||||
resourceName: string;
|
||||
hcEnabled: boolean;
|
||||
hcHealth: "unknown" | "healthy" | "unhealthy";
|
||||
}[];
|
||||
pagination: {
|
||||
total: number;
|
||||
limit: number;
|
||||
offset: number;
|
||||
};
|
||||
};
|
||||
|
||||
registry.registerPath({
|
||||
method: "get",
|
||||
path: "/org/{orgId}/health-checks",
|
||||
description: "List health checks for all resources in an organization.",
|
||||
tags: [OpenAPITags.Org, OpenAPITags.PublicResource],
|
||||
request: {
|
||||
params: listHealthChecksParamsSchema,
|
||||
query: listHealthChecksSchema
|
||||
},
|
||||
responses: {}
|
||||
});
|
||||
|
||||
export async function listHealthChecks(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedQuery = listHealthChecksSchema.safeParse(req.query);
|
||||
if (!parsedQuery.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedQuery.error)
|
||||
)
|
||||
);
|
||||
}
|
||||
const { limit, offset } = parsedQuery.data;
|
||||
|
||||
const parsedParams = listHealthChecksParamsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedParams.error)
|
||||
)
|
||||
);
|
||||
}
|
||||
const { orgId } = parsedParams.data;
|
||||
|
||||
const list = await db
|
||||
.select({
|
||||
targetHealthCheckId: targetHealthCheck.targetHealthCheckId,
|
||||
resourceId: resources.resourceId,
|
||||
resourceName: resources.name,
|
||||
hcEnabled: targetHealthCheck.hcEnabled,
|
||||
hcHealth: targetHealthCheck.hcHealth
|
||||
})
|
||||
.from(targetHealthCheck)
|
||||
.innerJoin(targets, eq(targets.targetId, targetHealthCheck.targetId))
|
||||
.innerJoin(resources, eq(resources.resourceId, targets.resourceId))
|
||||
.where(eq(resources.orgId, orgId))
|
||||
.orderBy(sql`${resources.name} ASC`)
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
|
||||
const [{ count }] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(targetHealthCheck)
|
||||
.innerJoin(targets, eq(targets.targetId, targetHealthCheck.targetId))
|
||||
.innerJoin(resources, eq(resources.resourceId, targets.resourceId))
|
||||
.where(eq(resources.orgId, orgId));
|
||||
|
||||
return response<ListHealthChecksResponse>(res, {
|
||||
data: {
|
||||
healthChecks: list.map((row) => ({
|
||||
targetHealthCheckId: row.targetHealthCheckId,
|
||||
resourceId: row.resourceId,
|
||||
resourceName: row.resourceName,
|
||||
hcEnabled: row.hcEnabled,
|
||||
hcHealth: (row.hcHealth ?? "unknown") as
|
||||
| "unknown"
|
||||
| "healthy"
|
||||
| "unhealthy"
|
||||
})),
|
||||
pagination: {
|
||||
total: count,
|
||||
limit,
|
||||
offset
|
||||
}
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Health checks retrieved successfully",
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -228,6 +228,7 @@ export async function createTarget(
|
||||
healthCheck = await db
|
||||
.insert(targetHealthCheck)
|
||||
.values({
|
||||
name: `${targetData.ip}:${targetData.port}`,
|
||||
targetId: newTarget[0].targetId,
|
||||
hcEnabled: targetData.hcEnabled ?? false,
|
||||
hcPath: targetData.hcPath ?? null,
|
||||
|
||||
Reference in New Issue
Block a user