From 57c329131ae7cc6f62d061788f169ddd9dc3e543 Mon Sep 17 00:00:00 2001 From: Leandro Facchinetti Date: Thu, 17 Nov 2022 19:16:23 +0000 Subject: [PATCH] --- server/index.mts | 75 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/server/index.mts b/server/index.mts index e3d10d8..be61942 100644 --- a/server/index.mts +++ b/server/index.mts @@ -286,10 +286,81 @@ await commander.program application.configuration.hstsPreload ??= false; application.configuration.caddy ??= caddyfile``; - application.web.get("/", (request, response) => { - response.send("TODO"); + application.log = (...messageParts) => { + console.log( + [ + new Date().toISOString(), + application.process.type, + application.process.number, + application.process.id, + ...messageParts, + ].join("\t") + ); + }; + + application.log( + "STARTED", + ...(application.process.type === "main" + ? [ + application.name, + application.version, + `https://${application.configuration.hostname}`, + ] + : []) + ); + + process.once("exit", () => { + application.log("STOPPED"); }); + type ResponseLocalsLogging = { + log(...messageParts: string[]): void; + }; + + application.web.enable("trust proxy"); + + application.web.use<{}, any, {}, {}, ResponseLocalsLogging>( + (request, response, next) => { + if (response.locals.log !== undefined) return next(); + + const id = Math.random().toString(36).slice(2); + const time = process.hrtime.bigint(); + response.locals.log = (...messageParts) => { + application.log( + id, + `${(process.hrtime.bigint() - time) / 1_000_000n}ms`, + request.ip, + request.method, + request.originalUrl, + ...messageParts + ); + }; + const log = response.locals.log; + + log("STARTING..."); + + response.once("close", () => { + const contentLength = response.getHeader("Content-Length"); + log( + "FINISHED", + String(response.statusCode), + ...(typeof contentLength === "string" + ? [`${Math.ceil(Number(contentLength) / 1000)}kB`] + : []) + ); + }); + + next(); + } + ); + + application.web.get<{}, any, {}, {}, ResponseLocalsLogging>( + "/", + (request, response) => { + response.send("TODO"); + } + ); + // #!/usr/bin/env node // import path from "path";