diff options
author | Alan Pearce | 2023-11-23 19:19:59 +0100 |
---|---|---|
committer | Alan Pearce | 2023-11-23 19:22:33 +0100 |
commit | 9fa5bde100dd2fbd88cf6ffad0c8e8ebbd72676f (patch) | |
tree | cb25f3ceece3cc51ed0d1d43cca2441f9c297140 /src | |
parent | 95e3c528594f6137d7d8310c8e4231283c35933b (diff) | |
download | website-9fa5bde100dd2fbd88cf6ffad0c8e8ebbd72676f.tar.lz website-9fa5bde100dd2fbd88cf6ffad0c8e8ebbd72676f.tar.zst website-9fa5bde100dd2fbd88cf6ffad0c8e8ebbd72676f.zip |
Log access
Diffstat (limited to 'src')
-rw-r--r-- | src/app.ts | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/src/app.ts b/src/app.ts index b2957aa..1290aaa 100644 --- a/src/app.ts +++ b/src/app.ts @@ -98,7 +98,7 @@ function walkDirectory(root: string, dir: string) { walkDirectory("public/", ""); async function serveFile( - file: File | undefined, + file: File, statusCode: number = 200, extraHeaders: Record<string, string> = {}, ): Promise<Response> { @@ -134,6 +134,7 @@ export const server = { 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 @@ export const server = { }); 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 @@ export const server = { 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 @@ export const server = { contentEncoding = "gzip"; suffix = ".gz"; } - const status = 200; + + status = 200; transaction.setHttpStatus(status); metrics.requests.inc({ method: request.method, @@ -197,7 +199,11 @@ export const server = { 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 @@ export const server = { 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; |