about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/index.ts82
1 files changed, 46 insertions, 36 deletions
diff --git a/src/index.ts b/src/index.ts
index f968fda..d979f43 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,10 +1,13 @@
 import path from "node:path";
 import fs, { Stats } from "node:fs";
 import type { BunFile, Serve } from "bun";
+import * as Sentry from "@sentry/node";
 import prom from "bun-prometheus-client";
 
 import readConfig from "./config";
 
+Sentry.init({});
+
 const base = "./website/";
 const publicDir = path.resolve(base, "public") + path.sep;
 
@@ -148,44 +151,51 @@ console.info(
 
 const server = Bun.serve({
   fetch: async function (request) {
-    const pathname = new URL(request.url).pathname;
-    const file = files.get(pathname);
-    counters.requestsByPath.inc({ path: pathname });
-    if (file) {
-      if (
-        parseIfModifiedSinceHeader(request.headers.get("if-modified-since")) >=
-        file?.mtime.getTime()
-      ) {
-        counters.requestsByStatus.inc({ status_code: 304 });
-        return new Response("", { status: 304, headers: defaultHeaders });
-      }
-      const encodings = (request.headers.get("accept-encoding") || "")
-        .split(",")
-        .map((x) => x.trim().toLowerCase());
-      if (encodings.includes("br") && files.has(file.relPath + ".br")) {
-        return serveEncodedFile(files.get(file.relPath + ".br"), 200, {
-          "content-encoding": "br",
-          "content-type": file.handle.type,
-        });
-      } else if (
-        encodings.includes("zstd") &&
-        files.has(file.relPath + ".zst")
-      ) {
-        return serveEncodedFile(files.get(file.relPath + ".zst"), 200, {
-          "content-encoding": "zstd",
-          "content-type": file.handle.type,
-        });
-      } else if (
-        encodings.includes("gzip") &&
-        files.has(file.relPath + ".gz")
-      ) {
-        return serveEncodedFile(files.get(file.relPath + ".gz"), 200, {
-          "content-encoding": "gzip",
-          "content-type": file.handle.type,
-        });
+    try {
+      const pathname = new URL(request.url).pathname;
+      const file = files.get(pathname);
+      counters.requestsByPath.inc({ path: pathname });
+      if (file) {
+        if (
+          parseIfModifiedSinceHeader(
+            request.headers.get("if-modified-since"),
+          ) >= file?.mtime.getTime()
+        ) {
+          counters.requestsByStatus.inc({ status_code: 304 });
+          return new Response("", { status: 304, headers: defaultHeaders });
+        }
+        const encodings = (request.headers.get("accept-encoding") || "")
+          .split(",")
+          .map((x) => x.trim().toLowerCase());
+        if (encodings.includes("br") && files.has(file.relPath + ".br")) {
+          return serveEncodedFile(files.get(file.relPath + ".br"), 200, {
+            "content-encoding": "br",
+            "content-type": file.handle.type,
+          });
+        } else if (
+          encodings.includes("zstd") &&
+          files.has(file.relPath + ".zst")
+        ) {
+          return serveEncodedFile(files.get(file.relPath + ".zst"), 200, {
+            "content-encoding": "zstd",
+            "content-type": file.handle.type,
+          });
+        } else if (
+          encodings.includes("gzip") &&
+          files.has(file.relPath + ".gz")
+        ) {
+          return serveEncodedFile(files.get(file.relPath + ".gz"), 200, {
+            "content-encoding": "gzip",
+            "content-type": file.handle.type,
+          });
+        }
       }
+      return serveFile(file);
+    } catch (error) {
+      counters.requestsByStatus.inc({ status_code: 503 });
+      Sentry.captureException(error);
+      return new Response("Something went wrong", { status: 503 });
     }
-    return serveFile(file);
   },
 });