Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
3.0 kB ยท 113 lines
TypeScript
at incident-api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";import { zValidator } from "@hono/zod-validator";import { db, eq } from "@openstatus/db";import { monitorIncidentTable } from "@openstatus/db/src/schema/monitor_incidents/monitor_incident";import { Receiver } from "@upstash/qstash";import { Hono } from "hono";import playwright from "playwright";import { z } from "zod";
import { env } from "./env";
const S3 = new S3Client({ region: "auto", endpoint: env.R2_URL, credentials: { accessKeyId: env.R2_ACCESS_KEY, secretAccessKey: env.R2_SECRET_KEY, },});
const receiver = new Receiver({ currentSigningKey: env.QSTASH_SIGNING_SECRET, nextSigningKey: env.QSTASH_NEXT_SIGNING_SECRET,});
const app = new Hono();
app.get("/ping", (c) => c.json({ ping: "pong", region: process.env.FLY_REGION }, 200),);
app.post( "/", zValidator( "json", z.object({ url: z.url(), incidentId: z.number(), kind: z.enum(["incident", "recovery"]), }), ), async (c) => { const signature = c.req.header("Upstash-Signature"); // if (auth !== `Basic ${env.HEADER_TOKEN}`) { // console.error("Unauthorized"); // return c.text("Unauthorized", 401); // }
const data = c.req.valid("json"); const isValid = receiver.verify({ signature: signature || "", body: JSON.stringify(data), }); if (!isValid) { console.error("Unauthorized"); return c.text("Unauthorized", 401); }
const browser = await playwright.chromium.launch({ headless: true, // set this to true });
try { const page = await browser.newPage(); await page.goto(data.url, { waitUntil: "load" }); const img = await page.screenshot({ fullPage: true }); const id = `${data.incidentId}-${Date.now()}.png`; const url = `https://screenshot.openstat.us/${id}`;
await S3.send( new PutObjectCommand({ Body: img, Bucket: "incident-screenshot", Key: id, ContentType: "image/png", }), );
if (data.kind === "incident") { await db .update(monitorIncidentTable) .set({ incidentScreenshotUrl: url }) .where(eq(monitorIncidentTable.id, data.incidentId)) .run(); } if (data.kind === "recovery") { await db .update(monitorIncidentTable) .set({ recoveryScreenshotUrl: url }) .where(eq(monitorIncidentTable.id, data.incidentId)) .run(); } } catch (e) { console.log("could not take screenshot timeout"); if (data.kind === "incident") { await db .update(monitorIncidentTable) .set({ incidentScreenshotUrl: "https://screenshot.openstat.us/err-connection-timed-out.jpg", }) .where(eq(monitorIncidentTable.id, data.incidentId)) .run(); } // console.log(e); }
return c.text("Screenshot saved"); },);
export default app;