diff --git a/apps/web/src/routes/dashboard.ts b/apps/web/src/routes/dashboard.ts index e089926..d7d2d4e 100644 --- a/apps/web/src/routes/dashboard.ts +++ b/apps/web/src/routes/dashboard.ts @@ -460,6 +460,44 @@ export const dashboard = new Elysia() return redirect("/dashboard/home"); }) + // Edit monitor via form POST + .post("/dashboard/monitors/:id/edit", async ({ cookie, headers, params, body }) => { + const resolved = await getAccountId(cookie, headers); + if (!resolved?.accountId) return redirect("/dashboard"); + + const b = body as any; + const regions = Array.isArray(b.regions) ? b.regions : (b.regions ? [b.regions] : []); + const query = b.query ? (typeof b.query === "string" ? JSON.parse(b.query) : b.query) : undefined; + const requestHeaders: Record = {}; + const hKeys = Array.isArray(b.header_key) ? b.header_key : (b.header_key ? [b.header_key] : []); + const hVals = Array.isArray(b.header_value) ? b.header_value : (b.header_value ? [b.header_value] : []); + for (let i = 0; i < hKeys.length; i++) { + if (hKeys[i]?.trim()) requestHeaders[hKeys[i].trim()] = hVals[i] || ""; + } + + try { + const apiUrl = process.env.API_URL || "https://api.pingql.com"; + const key = cookie?.pingql_key?.value; + await fetch(`${apiUrl}/monitors/${params.id}`, { + method: "PATCH", + headers: { "Content-Type": "application/json", "Authorization": `Bearer ${key}` }, + body: JSON.stringify({ + name: b.name, + url: b.url, + method: b.method || "GET", + interval_s: Number(b.interval_s) || 30, + timeout_ms: Number(b.timeout_ms) || 10000, + regions, + request_headers: Object.keys(requestHeaders).length ? requestHeaders : null, + request_body: b.request_body || null, + query, + }), + }); + } catch {} + + return redirect(`/dashboard/monitors/${params.id}`); + }) + // Delete monitor via form POST .post("/dashboard/monitors/:id/delete", async ({ cookie, headers, params }) => { const resolved = await getAccountId(cookie, headers); diff --git a/apps/web/src/views/partials/monitor-form.ejs b/apps/web/src/views/partials/monitor-form.ejs index 07860a4..cf36f68 100644 --- a/apps/web/src/views/partials/monitor-form.ejs +++ b/apps/web/src/views/partials/monitor-form.ejs @@ -17,7 +17,7 @@ const bodyHidden = ['GET','HEAD','OPTIONS'].includes(curMethod); %> -
+