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<()> {
client
.post(format!("{coordinator_url}/checks/ingest"))
.post(format!("{coordinator_url}/internal/ingest"))
.header("x-monitor-token", token)
.json(&result)
.send()

View File

@ -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: [] };

View File

@ -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);

View File

@ -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;
@ -34,20 +32,5 @@ export const checks = new Elysia()
cert_expiry_days: t.Optional(t.Nullable(t.Number())),
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 },
});

View File

@ -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"] } });