move to an env files, update depends

This commit is contained in:
creations 2025-01-16 12:22:45 -05:00
parent d0e9e15e97
commit 5687fb4bec
Signed by: creations
GPG key ID: 8F553AA4320FC711
6 changed files with 80 additions and 23 deletions

3
.env Normal file
View file

@ -0,0 +1,3 @@
# NODE_ENV=development
HOST=0.0.0.0
PORT=8080

View file

@ -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"),
};

View file

@ -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",

View file

@ -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,

View file

@ -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<Response> {
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;
}
}

4
types/bun.d.ts vendored
View file

@ -2,4 +2,8 @@ import type { Server } from "bun";
declare global {
type BunServer = Server;
type ExtendedRequest = Request & {
startPerf: number;
};
}