feat: custom method, headers, body, timeout on monitors

This commit is contained in:
M1
2026-03-16 15:30:35 +04:00
parent d98aa5e46f
commit 3368dbdd7f
6 changed files with 173 additions and 45 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ export const internal = new Elysia({ prefix: "/internal", detail: { hide: true }
.get("/due", async () => {
return sql`
SELECT m.id, m.url, m.interval_s, m.query
SELECT m.id, m.url, m.method, m.request_headers, m.request_body, m.timeout_ms, m.interval_s, m.query
FROM monitors m
LEFT JOIN LATERAL (
SELECT checked_at FROM pings
+26 -10
View File
@@ -3,10 +3,14 @@ import { requireAuth } from "./auth";
import sql from "../db";
const MonitorBody = t.Object({
name: t.String({ description: "Human-readable name" }),
url: t.String({ format: "uri", description: "URL to check" }),
interval_s: t.Optional(t.Number({ minimum: 10, default: 60, description: "Check interval in seconds" })),
query: t.Optional(t.Any({ description: "PingQL query — filter conditions for up/down" })),
name: t.String({ description: "Human-readable name" }),
url: t.String({ format: "uri", description: "URL to check" }),
method: t.Optional(t.String({ default: "GET", description: "HTTP method: GET, POST, PUT, PATCH, DELETE, HEAD" })),
request_headers: t.Optional(t.Any({ description: "Request headers as key-value object" })),
request_body: t.Optional(t.Nullable(t.String({ description: "Request body for POST/PUT/PATCH" }))),
timeout_ms: t.Optional(t.Number({ minimum: 1000, maximum: 60000, default: 30000, description: "Request timeout in ms" })),
interval_s: t.Optional(t.Number({ minimum: 10, default: 60, description: "Check interval in seconds" })),
query: t.Optional(t.Any({ description: "PingQL query — filter conditions for up/down" })),
});
export const monitors = new Elysia({ prefix: "/monitors" })
@@ -20,8 +24,16 @@ export const monitors = new Elysia({ prefix: "/monitors" })
// Create monitor
.post("/", async ({ accountId, body }) => {
const [monitor] = await sql`
INSERT INTO monitors (account_id, name, url, interval_s, query)
VALUES (${accountId}, ${body.name}, ${body.url}, ${body.interval_s ?? 60}, ${body.query ? sql.json(body.query) : null})
INSERT INTO monitors (account_id, name, url, method, request_headers, request_body, timeout_ms, interval_s, query)
VALUES (
${accountId}, ${body.name}, ${body.url},
${(body.method ?? 'GET').toUpperCase()},
${body.request_headers ? sql.json(body.request_headers) : null},
${body.request_body ?? null},
${body.timeout_ms ?? 30000},
${body.interval_s ?? 60},
${body.query ? sql.json(body.query) : null}
)
RETURNING *
`;
return monitor;
@@ -45,10 +57,14 @@ export const monitors = new Elysia({ prefix: "/monitors" })
.patch("/:id", async ({ accountId, params, body, error }) => {
const [monitor] = await sql`
UPDATE monitors SET
name = COALESCE(${body.name ?? null}, name),
url = COALESCE(${body.url ?? null}, url),
interval_s = COALESCE(${body.interval_s ?? null}, interval_s),
query = COALESCE(${body.query ? sql.json(body.query) : null}, query)
name = COALESCE(${body.name ?? null}, name),
url = COALESCE(${body.url ?? null}, url),
method = COALESCE(${body.method ? body.method.toUpperCase() : null}, method),
request_headers = COALESCE(${body.request_headers ? sql.json(body.request_headers) : null}, request_headers),
request_body = COALESCE(${body.request_body ?? null}, request_body),
timeout_ms = COALESCE(${body.timeout_ms ?? null}, timeout_ms),
interval_s = COALESCE(${body.interval_s ?? null}, interval_s),
query = COALESCE(${body.query ? sql.json(body.query) : null}, query)
WHERE id = ${params.id} AND account_id = ${accountId}
RETURNING *
`;