refactor: merge checks into monitors (/monitors/:id/history), ingest moves to /internal/ingest

This commit is contained in:
M1 2026-03-16 13:43:55 +04:00
parent a22112dc77
commit b4f95fa375
5 changed files with 30 additions and 33 deletions

View File

@ -168,7 +168,7 @@ async fn post_result(
result: CheckResult, result: CheckResult,
) -> Result<()> { ) -> Result<()> {
client client
.post(format!("{coordinator_url}/checks/ingest")) .post(format!("{coordinator_url}/internal/ingest"))
.header("x-monitor-token", token) .header("x-monitor-token", token)
.json(&result) .json(&result)
.send() .send()

View File

@ -58,7 +58,7 @@
const monitorsWithChecks = await Promise.all( const monitorsWithChecks = await Promise.all(
monitors.map(async (m) => { monitors.map(async (m) => {
try { try {
const checks = await api(`/checks/${m.id}?limit=20`); const checks = await api(`/monitors/${m.id}/history?limit=20`);
return { ...m, checks }; return { ...m, checks };
} catch { } catch {
return { ...m, checks: [] }; return { ...m, checks: [] };

View File

@ -1,7 +1,7 @@
import { Elysia } from "elysia"; import { Elysia } from "elysia";
import { cors } from "@elysiajs/cors"; import { cors } from "@elysiajs/cors";
import { swagger } from "@elysiajs/swagger"; import { swagger } from "@elysiajs/swagger";
import { checks } from "./routes/checks"; import { ingest } from "./routes/checks";
import { monitors } from "./routes/monitors"; import { monitors } from "./routes/monitors";
import { account } from "./routes/auth"; import { account } from "./routes/auth";
import { internal } from "./routes/internal"; import { internal } from "./routes/internal";
@ -17,7 +17,7 @@ const app = new Elysia()
.use(dashboard) .use(dashboard)
.use(account) .use(account)
.use(monitors) .use(monitors)
.use(checks) .use(ingest)
.use(internal) .use(internal)
.listen(3000); .listen(3000);

View File

@ -1,14 +1,12 @@
import { Elysia, t } from "elysia"; import { Elysia, t } from "elysia";
import { requireAuth } from "./auth";
import sql from "../db"; import sql from "../db";
export const checks = new Elysia() // Internal-only: called by the Rust monitor runner
// Public-ish: token protected, no user auth export const ingest = new Elysia()
.post("/checks/ingest", async ({ body, headers, error }) => { .post("/internal/ingest", async ({ body, headers, error }) => {
const token = headers["x-monitor-token"]; const token = headers["x-monitor-token"];
if (token !== process.env.MONITOR_TOKEN) return error(401, { error: "Unauthorized" }); if (token !== process.env.MONITOR_TOKEN) return error(401, { error: "Unauthorized" });
// Merge cert_expiry_days into meta if present
const meta = body.meta ? { ...body.meta } : {}; const meta = body.meta ? { ...body.meta } : {};
if (body.cert_expiry_days != null) meta.cert_expiry_days = body.cert_expiry_days; if (body.cert_expiry_days != null) meta.cert_expiry_days = body.cert_expiry_days;
@ -34,20 +32,5 @@ export const checks = new Elysia()
cert_expiry_days: t.Optional(t.Nullable(t.Number())), cert_expiry_days: t.Optional(t.Nullable(t.Number())),
meta: t.Optional(t.Any()), meta: t.Optional(t.Any()),
}), }),
detail: { summary: "Ingest result (monitor runner)", tags: ["internal"] }, detail: { hide: true },
}) });
// User auth required below
.use(requireAuth)
.get("/checks/:monitorId", async ({ accountId, params, query, error }) => {
const [monitor] = await sql`
SELECT id FROM monitors WHERE id = ${params.monitorId} AND account_id = ${accountId}
`;
if (!monitor) return error(404, { error: "Not found" });
const limit = Math.min(Number(query.limit ?? 100), 1000);
return sql`
SELECT * FROM check_results
WHERE monitor_id = ${params.monitorId}
ORDER BY checked_at DESC LIMIT ${limit}
`;
}, { detail: { summary: "Get check history", tags: ["checks"] } });

View File

@ -74,4 +74,18 @@ export const monitors = new Elysia({ prefix: "/monitors" })
`; `;
if (!monitor) return error(404, { error: "Not found" }); if (!monitor) return error(404, { error: "Not found" });
return monitor; return monitor;
}, { detail: { summary: "Toggle monitor on/off", tags: ["monitors"] } }); }, { detail: { summary: "Toggle monitor on/off", tags: ["monitors"] } })
// Check history
.get("/:id/history", async ({ accountId, params, query, error }) => {
const [monitor] = await sql`
SELECT id FROM monitors WHERE id = ${params.id} AND account_id = ${accountId}
`;
if (!monitor) return error(404, { error: "Not found" });
const limit = Math.min(Number(query.limit ?? 100), 1000);
return sql`
SELECT * FROM check_results
WHERE monitor_id = ${params.id}
ORDER BY checked_at DESC LIMIT ${limit}
`;
}, { detail: { summary: "Get check history", tags: ["monitors"] } });