all repos — homestead @ 93866314aac91b2b9d0ef7ce4c83204f77b51748

Code for my website

Log access

Alan Pearce
commit

93866314aac91b2b9d0ef7ce4c83204f77b51748

parent

16688c921d6649609228b6cdba2e10f67bbd498c

1 file changed, 23 insertions(+), 16 deletions(-)

jump to
M src/app.tssrc/app.ts
@@ -98,7 +98,7 @@
walkDirectory("public/", ""); async function serveFile( - file: File | undefined, + file: File, statusCode: number = 200, extraHeaders: Record<string, string> = {}, ): Promise<Response> {
@@ -134,6 +134,7 @@ fetch: async function (request) {
const url = new URL(request.url); const pathname = url.pathname.replace(/\/\/+/g, "/"); const endTimer = metrics.requestDuration.startTimer({ path: pathname }); + let status; const transaction = Sentry.startTransaction({ name: pathname, op: "http.server",
@@ -147,13 +148,13 @@ },
}); try { if (pathname === "/health") { - return new Response("OK", { status: 200 }); + return new Response("OK", { status: (status = 200) }); } else if ( config.redirect_other_hostnames && request.headers.get("host") !== expectedHostURL.host ) { return new Response("", { - status: 301, + status: (status = 301), headers: { location: new URL(pathname, expectedHostURL).toString(), },
@@ -171,10 +172,10 @@ ) {
metrics.requests.inc({ method: request.method, path: pathname, - status_code: 304, + status_code: (status = 304), }); transaction.setHttpStatus(304); - return new Response("", { status: 304, headers: defaultHeaders }); + return new Response("", { status: status, headers: defaultHeaders }); } const encodings = (request.headers.get("accept-encoding") || "") .split(",")
@@ -189,7 +190,8 @@ } else if (encodings.includes("gzip") && files.has(pathname + ".gz")) {
contentEncoding = "gzip"; suffix = ".gz"; } - const status = 200; + + status = 200; transaction.setHttpStatus(status); metrics.requests.inc({ method: request.method,
@@ -197,7 +199,11 @@ path: pathname,
status_code: status, content_encoding: contentEncoding, }); - return serveFile(files.get(pathname + suffix), status, { + const endFile = files.get(pathname + suffix); + if (!endFile) { + throw new Error(`File ${pathname} not found`); + } + return serveFile(endFile, status, { "content-encoding": contentEncoding, "content-type": file.type, });
@@ -207,47 +213,48 @@ log.info(`Redirecting to: ${pathname + "/"}`);
metrics.requests.inc({ method: request.method, path: pathname, - status_code: 302, + status_code: (status = 302), }); return new Response("", { - status: 302, + status: status, headers: { location: pathname + "/" }, }); } metrics.requests.inc({ method: request.method, path: pathname, - status_code: 404, + status_code: (status = 404), content_encoding: "identity", }); - transaction.setHttpStatus(404); + transaction.setHttpStatus(status); const notfound = files.get("/404.html"); if (notfound) { - return serveFile(notfound, 404, { + return serveFile(notfound, status, { "content-type": "text/html; charset=utf-8", }); } else { log.warn("404.html not found"); return new Response("404 Not Found", { - status: 404, + status: status, headers: { "content-type": "text/plain", ...defaultHeaders }, }); } } } catch (error) { - transaction.setHttpStatus(503); + transaction.setHttpStatus((status = 503)); metrics.requests.inc({ method: request.method, path: pathname, - status_code: 503, + status_code: status, content_encoding: "identity", }); Sentry.captureException(error); - return new Response("Something went wrong", { status: 503 }); + return new Response("Something went wrong", { status: status }); } finally { const seconds = endTimer(); metrics.requestDuration.observe(seconds); transaction.finish(); + console.log(request.method, status, pathname); } }, } satisfies Serve;