From 5687fb4becf9a8179a20196701f039c82030371e Mon Sep 17 00:00:00 2001 From: creations Date: Thu, 16 Jan 2025 12:22:45 -0500 Subject: [PATCH] move to an env files, update depends --- .env | 3 +++ config/environment.ts | 8 +++---- package.json | 12 +++++------ src/helpers/logger.ts | 27 ++++++++++++++++++++++++ src/server.ts | 49 +++++++++++++++++++++++++++++++------------ types/bun.d.ts | 4 ++++ 6 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..b069881 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +# NODE_ENV=development +HOST=0.0.0.0 +PORT=8080 diff --git a/config/environment.ts b/config/environment.ts index f5de457..4b701df 100644 --- a/config/environment.ts +++ b/config/environment.ts @@ -1,7 +1,7 @@ export const environment: Environment = { - port: 6600, - host: "127.0.0.1", + port: parseInt(process.env.PORT || "8080", 10), + host: process.env.HOST || "0.0.0.0", development: - process.argv.includes("--dev") || - process.argv.includes("--development"), + process.env.NODE_ENV === "development" || + process.argv.includes("--dev"), }; diff --git a/package.json b/package.json index c4a3666..de82eef 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,13 @@ "cleanup": "rm -rf logs node_modules bun.lockdb" }, "devDependencies": { - "@eslint/js": "^9.17.0", - "@types/bun": "^1.1.14", + "@eslint/js": "^9.18.0", + "@types/bun": "^1.1.16", "@types/ejs": "^3.1.5", - "@typescript-eslint/eslint-plugin": "^8.18.1", - "@typescript-eslint/parser": "^8.18.1", - "eslint": "^9.17.0", - "eslint-plugin-prettier": "^5.2.1", + "@typescript-eslint/eslint-plugin": "^8.20.0", + "@typescript-eslint/parser": "^8.20.0", + "eslint": "^9.18.0", + "eslint-plugin-prettier": "^5.2.2", "eslint-plugin-promise": "^7.2.1", "eslint-plugin-simple-import-sort": "^12.1.1", "eslint-plugin-unicorn": "^56.0.1", diff --git a/src/helpers/logger.ts b/src/helpers/logger.ts index 4f67130..c21aee7 100644 --- a/src/helpers/logger.ts +++ b/src/helpers/logger.ts @@ -157,6 +157,33 @@ class Logger { this.writeConsoleMessageColored(logMessageParts, breakLine); } + public custom( + bracketMessage: string, + bracketMessage2: string, + message: string | string[], + color: string, + breakLine: boolean = false, + ): void { + const stack: string = new Error().stack || ""; + const { timestamp } = this.getCallerInfo(stack); + + const joinedMessage: string = Array.isArray(message) + ? message.join(" ") + : message; + + const logMessageParts: ILogMessageParts = { + readableTimestamp: { value: timestamp, color: "90" }, + level: { value: bracketMessage, color }, + filename: { value: `${bracketMessage2}`, color: "36" }, + message: { value: joinedMessage, color: "0" }, + }; + + this.writeToLog( + `${timestamp} ${bracketMessage} (${bracketMessage2}) ${joinedMessage}`, + ); + this.writeConsoleMessageColored(logMessageParts, breakLine); + } + private writeConsoleMessageColored( logMessageParts: ILogMessageParts, breakLine: boolean = false, diff --git a/src/server.ts b/src/server.ts index 3f92824..b8b3f7a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,7 +6,6 @@ import { type MatchedRoute, type Serve, } from "bun"; -import { platform } from "os"; import { resolve } from "path"; class ServerHandler { @@ -57,15 +56,11 @@ class ServerHandler { let filePath: string; if (pathname === "/favicon.ico") { - filePath = resolve("./public/assets/favicon.ico"); + filePath = resolve("public", "assets", "favicon.ico"); } else { filePath = resolve(`.${pathname}`); } - if (platform() === "win32") { - filePath = filePath.replace(/\//g, "\\"); - } - const file: BunFile = Bun.file(filePath); if (await file.exists()) { @@ -88,9 +83,11 @@ class ServerHandler { } private async handleRequest( - request: Request, + request: ExtendedRequest, server: BunServer, ): Promise { + request.startPerf = performance.now(); + const pathname: string = new URL(request.url).pathname; if (pathname.startsWith("/public") || pathname === "/favicon.ico") { return await this.serveStaticFile(pathname); @@ -136,7 +133,7 @@ class ServerHandler { { success: false, code: 405, - error: `Method ${request.method} Not Allowed`, + error: `Method ${request.method} Not Allowed, expected ${routeModule.routeDef.method}`, }, { status: 405 }, ); @@ -153,7 +150,7 @@ class ServerHandler { { success: false, code: 406, - error: `Content-Type ${contentType} Not Acceptable`, + error: `Content-Type ${contentType} Not Acceptable, expected ${expectedContentType}`, }, { status: 406 }, ); @@ -166,10 +163,12 @@ class ServerHandler { params, ); - response.headers.set( - "Content-Type", - routeModule.routeDef.returns, - ); + if (routeModule.routeDef.returns !== "*/*") { + response.headers.set( + "Content-Type", + routeModule.routeDef.returns, + ); + } } } } catch (error: unknown) { @@ -196,6 +195,30 @@ class ServerHandler { ); } + queueMicrotask(() => { + const headers: Headers = response.headers; + let ip: string | null = server.requestIP(request)?.address || null; + + if (!ip) { + ip = + headers.get("CF-Connecting-IP") || + headers.get("X-Real-IP") || + headers.get("X-Forwarded-For") || + null; + } + + logger.custom( + `[${request.method}]`, + `(${response.status})`, + [ + request.url, + `${(performance.now() - request.startPerf).toFixed(2)}ms`, + ip || "unknown", + ], + "90", + ); + }); + return response; } } diff --git a/types/bun.d.ts b/types/bun.d.ts index 741e55c..240b1d7 100644 --- a/types/bun.d.ts +++ b/types/bun.d.ts @@ -2,4 +2,8 @@ import type { Server } from "bun"; declare global { type BunServer = Server; + + type ExtendedRequest = Request & { + startPerf: number; + }; }