all repos — website @ 3a2d198d153efc8a69e7640f7dcde88207268ff3

My website

Replace zola with DOM-based static site generation code

Alan Pearce
commit

3a2d198d153efc8a69e7640f7dcde88207268ff3

parent

9c4fd37c97a87b9d1ab9e65b77e4578936b66323

1 file changed, 34 insertions(+), 8 deletions(-)

changed files
M src/app.tssrc/app.ts
@@ -3,17 +3,17 @@ 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 log from "loglevel"; + +import config from "./config"; -import readConfig from "./config"; +log.setLevel((Bun.env.LOG_LEVEL || "info") as log.LogLevelDesc); Sentry.init({ release: `homestead@${Bun.env.FLY_MACHINE_VERSION}`, tracesSampleRate: 1.0, }); -const publicDir = "public" + path.sep; - -const config = readConfig(); const defaultHeaders = { ...config.extra.headers, vary: "Accept-Encoding",
@@ -85,13 +85,14 @@ registerFile(relPath, dir, absPath, stat);
if (dir !== "") { registerFile(relPath, dir + path.sep, absPath, stat); } + } else { + registerFile(relPath, relPath, absPath, stat); } - registerFile(relPath, relPath, absPath, stat); } } } -walkDirectory(publicDir, ""); +walkDirectory("public/", ""); async function serveFile( file: File | undefined,
@@ -194,8 +195,22 @@ });
} transaction.setHttpStatus(200); transaction.setTag("http.content-encoding", "identity"); - return serveFile(file); + return serveFile(file, 200, { + "content-type": file.type, + }); } else { + if (files.has(pathname + "/")) { + log.info(`Redirecting to: ${pathname + "/"}`); + metrics.requests.inc({ + method: request.method, + path: pathname, + status_code: 302, + }); + return new Response("", { + status: 302, + headers: { location: pathname + "/" }, + }); + } metrics.requests.inc({ method: request.method, path: pathname,
@@ -203,7 +218,18 @@ status_code: 404,
}); transaction.setHttpStatus(404); transaction.setTag("http.content-encoding", "identity"); - return serveFile(files.get("/404.html"), 404); + const notfound = files.get("/404.html"); + if (notfound) { + return serveFile(notfound, 404, { + "content-type": "text/html; charset=utf-8", + }); + } else { + log.warn("404.html not found"); + return new Response("404 Not Found", { + status: 404, + headers: { "content-type": "text/plain", ...defaultHeaders }, + }); + } } } catch (error) { transaction.setTag("http.content-encoding", "identity");