diff --git a/apps/monitor/src/runner.rs b/apps/monitor/src/runner.rs index 8f24ef5..90d1420 100644 --- a/apps/monitor/src/runner.rs +++ b/apps/monitor/src/runner.rs @@ -168,7 +168,7 @@ async fn post_result( result: CheckResult, ) -> Result<()> { client - .post(format!("{coordinator_url}/checks/ingest")) + .post(format!("{coordinator_url}/internal/ingest")) .header("x-monitor-token", token) .json(&result) .send() diff --git a/apps/web/src/dashboard/home.html b/apps/web/src/dashboard/home.html index 16a4b63..de6edd8 100644 --- a/apps/web/src/dashboard/home.html +++ b/apps/web/src/dashboard/home.html @@ -58,7 +58,7 @@ const monitorsWithChecks = await Promise.all( monitors.map(async (m) => { try { - const checks = await api(`/checks/${m.id}?limit=20`); + const checks = await api(`/monitors/${m.id}/history?limit=20`); return { ...m, checks }; } catch { return { ...m, checks: [] }; diff --git a/apps/web/src/index.ts b/apps/web/src/index.ts index 2d3d1cd..aaad692 100644 --- a/apps/web/src/index.ts +++ b/apps/web/src/index.ts @@ -1,7 +1,7 @@ import { Elysia } from "elysia"; import { cors } from "@elysiajs/cors"; import { swagger } from "@elysiajs/swagger"; -import { checks } from "./routes/checks"; +import { ingest } from "./routes/checks"; import { monitors } from "./routes/monitors"; import { account } from "./routes/auth"; import { internal } from "./routes/internal"; @@ -17,7 +17,7 @@ const app = new Elysia() .use(dashboard) .use(account) .use(monitors) - .use(checks) + .use(ingest) .use(internal) .listen(3000); diff --git a/apps/web/src/routes/checks.ts b/apps/web/src/routes/checks.ts index 59fe5f2..f335c3a 100644 --- a/apps/web/src/routes/checks.ts +++ b/apps/web/src/routes/checks.ts @@ -1,14 +1,12 @@ import { Elysia, t } from "elysia"; -import { requireAuth } from "./auth"; import sql from "../db"; -export const checks = new Elysia() - // Public-ish: token protected, no user auth - .post("/checks/ingest", async ({ body, headers, error }) => { +// Internal-only: called by the Rust monitor runner +export const ingest = new Elysia() + .post("/internal/ingest", async ({ body, headers, error }) => { const token = headers["x-monitor-token"]; 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 } : {}; if (body.cert_expiry_days != null) meta.cert_expiry_days = body.cert_expiry_days; @@ -26,28 +24,13 @@ export const checks = new Elysia() return { ok: true }; }, { body: t.Object({ - monitor_id: t.String(), - status_code: t.Optional(t.Number()), - latency_ms: t.Optional(t.Number()), - up: t.Boolean(), - error: t.Optional(t.Nullable(t.String())), + monitor_id: t.String(), + status_code: t.Optional(t.Number()), + latency_ms: t.Optional(t.Number()), + up: t.Boolean(), + error: t.Optional(t.Nullable(t.String())), 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"] }, - }) - - // 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"] } }); + detail: { hide: true }, + }); diff --git a/apps/web/src/routes/monitors.ts b/apps/web/src/routes/monitors.ts index 87ab540..9eaba23 100644 --- a/apps/web/src/routes/monitors.ts +++ b/apps/web/src/routes/monitors.ts @@ -74,4 +74,18 @@ export const monitors = new Elysia({ prefix: "/monitors" }) `; if (!monitor) return error(404, { error: "Not found" }); 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"] } });