about summary refs log tree commit diff stats
path: root/src/app.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.ts')
-rw-r--r--src/app.ts18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/app.ts b/src/app.ts
index e33bc72..8ba2d8a 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -14,6 +14,9 @@ Sentry.init({
   tracesSampleRate: 1.0,
 });
 
+const expectedHostURL = new URL(
+  Bun.env.NODE_ENV === "production" ? config.base_url : "http://localhost:3000",
+);
 const defaultHeaders = {
   ...config.extra.headers,
   vary: "Accept-Encoding",
@@ -128,7 +131,8 @@ export const metricsServer = {
 
 export const server = {
   fetch: async function (request) {
-    const pathname = new URL(request.url).pathname.replace(/\/\/+/g, "/");
+    const url = new URL(request.url);
+    const pathname = url.pathname.replace(/\/\/+/g, "/");
     const endTimer = metrics.requestDuration.startTimer({ path: pathname });
     const transaction = Sentry.startTransaction({
       name: pathname,
@@ -136,10 +140,22 @@ export const server = {
       description: `${request.method} ${pathname}`,
       tags: {
         url: request.url,
+        "http.host": request.headers.get("host"),
         "http.method": request.method,
         "http.user_agent": request.headers.get("user-agent"),
       },
     });
+    if (
+      config.redirect_other_hostnames &&
+      request.headers.get("host") !== expectedHostURL.host
+    ) {
+      return new Response("", {
+        status: 301,
+        headers: {
+          location: new URL(pathname, expectedHostURL).toString(),
+        },
+      });
+    }
     try {
       const file = files.get(pathname);
       if (file && (await file.handle.exists())) {