fix clan badge ( primary_guild), add timezone api, move robots to public/custom support, move to atums/echo for logging instead

This commit is contained in:
creations 2025-05-30 19:11:29 -04:00
parent eb2bec6649
commit 3cb3b76a2b
Signed by: creations
GPG key ID: 8F553AA4320FC711
13 changed files with 269 additions and 123 deletions

View file

@ -1,6 +1,6 @@
import { serverHandler } from "@/server";
import { echo } from "@atums/echo";
import { verifyRequiredVariables } from "@config/environment";
import { logger } from "@creations.works/logger";
async function main(): Promise<void> {
verifyRequiredVariables();
@ -8,7 +8,7 @@ async function main(): Promise<void> {
}
main().catch((error: Error) => {
logger.error(["Error initializing the server:", error]);
echo.error({ message: "Error initializing the server", error });
process.exit(1);
});

View file

@ -4,6 +4,7 @@ import {
lanyardConfig,
plausibleScript,
reviewDb,
timezoneAPIUrl,
} from "@config/environment";
import { file } from "bun";
@ -33,6 +34,10 @@ async function handler(request: ExtendedRequest): Promise<Response> {
head.setAttribute("data-review-db", reviewDb.url);
}
if (timezoneAPIUrl) {
head.setAttribute("data-timezone-api", timezoneAPIUrl);
}
if (plausibleScript) {
head.append(plausibleScript, { html: true });
}

View file

@ -1,6 +1,6 @@
import { resolve } from "node:path";
import { environment, robotstxtPath } from "@config/environment";
import { logger } from "@creations.works/logger";
import { echo } from "@atums/echo";
import { environment } from "@config/environment";
import {
type BunFile,
FileSystemRouter,
@ -43,16 +43,14 @@ class ServerHandler {
`http://127.0.0.1:${server.port}`,
];
logger.info(`Server running at ${accessUrls[0]}`);
logger.info(`Access via: ${accessUrls[1]} or ${accessUrls[2]}`, {
breakLine: true,
});
echo.info(`Server running at ${accessUrls[0]}`);
echo.info(`Access via: ${accessUrls[1]} or ${accessUrls[2]}`);
this.logRoutes();
}
private logRoutes(): void {
logger.info("Available routes:");
echo.info("Available routes:");
const sortedRoutes: [string, string][] = Object.entries(
this.router.routes,
@ -61,7 +59,7 @@ class ServerHandler {
);
for (const [path, filePath] of sortedRoutes) {
logger.info(`Route: ${path}, File: ${filePath}`);
echo.info(`Route: ${path}, File: ${filePath}`);
}
}
@ -90,11 +88,14 @@ class ServerHandler {
headers: { "Content-Type": contentType },
});
} else {
logger.warn(`File not found: ${filePath}`);
echo.warn(`File not found: ${filePath}`);
response = new Response("Not Found", { status: 404 });
}
} catch (error) {
logger.error([`Error serving static file: ${pathname}`, error as Error]);
echo.error({
message: `Error serving static file: ${pathname}`,
error: error as Error,
});
response = new Response("Internal Server Error", { status: 500 });
}
@ -107,16 +108,23 @@ class ServerHandler {
response: Response,
ip: string | undefined,
): void {
logger.custom(
`[${request.method}]`,
`(${response.status})`,
[
request.url,
`${(performance.now() - request.startPerf).toFixed(2)}ms`,
ip || "unknown",
],
"90",
);
const pathname = new URL(request.url).pathname;
const ignoredStartsWith: string[] = ["/public"];
const ignoredPaths: string[] = ["/favicon.ico"];
if (
ignoredStartsWith.some((prefix) => pathname.startsWith(prefix)) ||
ignoredPaths.includes(pathname)
) {
return;
}
echo.custom(`${request.method}`, `${response.status}`, [
request.url,
`${(performance.now() - request.startPerf).toFixed(2)}ms`,
ip || "unknown",
]);
}
private async handleRequest(
@ -139,29 +147,21 @@ class ServerHandler {
}
const pathname: string = new URL(request.url).pathname;
if (pathname === "/robots.txt" && robotstxtPath) {
try {
const file: BunFile = Bun.file(robotstxtPath);
if (await file.exists()) {
const fileContent: ArrayBuffer = await file.arrayBuffer();
const contentType: string = file.type || "text/plain";
const baseDir = resolve("public/custom");
const customPath = resolve(baseDir, pathname.slice(1));
response = new Response(fileContent, {
headers: { "Content-Type": contentType },
});
} else {
logger.warn(`File not found: ${robotstxtPath}`);
response = new Response("Not Found", { status: 404 });
}
} catch (error) {
logger.error([
`Error serving robots.txt: ${robotstxtPath}`,
error as Error,
]);
response = new Response("Internal Server Error", { status: 500 });
}
if (!customPath.startsWith(baseDir)) {
return new Response("Forbidden", { status: 403 });
}
const customFile = Bun.file(customPath);
if (await customFile.exists()) {
const content = await customFile.arrayBuffer();
const type = customFile.type || "application/octet-stream";
response = new Response(content, {
headers: { "Content-Type": type },
});
this.logRequest(extendedRequest, response, ip);
return response;
}
@ -269,7 +269,10 @@ class ServerHandler {
}
}
} catch (error: unknown) {
logger.error([`Error handling route ${request.url}:`, error as Error]);
echo.error({
message: `Error handling route ${request.url}`,
error: error,
});
response = Response.json(
{
@ -291,9 +294,11 @@ class ServerHandler {
);
}
this.logRequest(extendedRequest, response, ip);
return response;
}
}
const serverHandler: ServerHandler = new ServerHandler(
environment.port,
environment.host,

View file

@ -1,74 +1,73 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="View real-time Discord presence, activities, and badges with open-source integration." />
<meta name="color-scheme" content="dark" />
<title>Discord Presence</title>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description"
content="View real-time Discord presence, activities, and badges with open-source integration." />
<meta name="color-scheme" content="dark" />
<link rel="icon" id="site-icon" type="image/png" href="/public/assets/favicon.png" />
<link rel="stylesheet" href="/public/css/index.css" />
<link rel="stylesheet" href="/public/css/root.css" />
</head>
<title>Discord Presence</title>
<body>
<div id="loading-overlay" role="status" aria-live="polite">
<div class="loading-spinner"></div>
<link rel="icon" id="site-icon" type="image/png" href="/public/assets/favicon.png" />
<link rel="stylesheet" href="/public/css/index.css" />
<link rel="stylesheet" href="/public/css/root.css" />
</head>
<body>
<div id="loading-overlay" role="status" aria-live="polite">
<div class="loading-spinner"></div>
</div>
<header>
<a href="https://git.creations.works/creations/profilePage" target="_blank" rel="noopener noreferrer"
title="View source code on Forgejo">
<img class="open-source-logo" src="/public/assets/forgejo_logo.svg" alt="Forgejo open-source logo"
style="opacity: 0.5" loading="lazy" />
</a>
<div class="timezone-wrapper" id="timezone-wrapper" aria-label="Timezone Information">
<span class="timezone-label">Users Time:</span>
<span class="timezone-value"></span>
</div>
</header>
<header>
<a
href="https://git.creations.works/creations/profilePage"
target="_blank"
rel="noopener noreferrer"
title="View source code on Forgejo"
>
<img
class="open-source-logo"
src="/public/assets/forgejo_logo.svg"
alt="Forgejo open-source logo"
style="opacity: 0.5"
loading="lazy"
/>
</a>
</header>
<main>
<section class="user-card">
<div class="avatar-status-wrapper">
<div class="avatar-wrapper">
<img class="avatar hidden"/>
<img class="decoration hidden"/>
<div class="status-indicator offline hidden"></div>
</div>
<div class="user-info">
<div class="user-info-inner">
<h1 class="username"></h1>
</div>
<main>
<section class="user-card">
<div class="avatar-status-wrapper">
<div class="avatar-wrapper">
<img class="avatar hidden" />
<img class="decoration hidden" />
<div class="status-indicator offline hidden"></div>
</div>
<div class="user-info">
<div class="user-info-inner">
<h1 class="username"></h1>
</div>
</div>
</section>
<section id="badges" class="badges hidden" aria-label="User Badges"></section>
<section aria-label="Discord Activities" class="activities-section">
<h2 class="activity-block-header hidden">Activities</h2>
<ul class="activities"></ul>
</section>
<section class="readme hidden" aria-label="Profile README">
<div class="markdown-body"></div>
</section>
</main>
<section class="reviews hidden" aria-label="User Reviews">
<h2>User Reviews</h2>
<ul class="reviews-list">
</ul>
</div>
</section>
<script src="/public/js/index.js" type="module"></script>
</body>
</html>
<section id="badges" class="badges hidden" aria-label="User Badges"></section>
<section aria-label="Discord Activities" class="activities-section">
<h2 class="activity-block-header hidden">Activities</h2>
<ul class="activities"></ul>
</section>
<section class="readme hidden" aria-label="Profile README">
<div class="markdown-body"></div>
</section>
</main>
<section class="reviews hidden" aria-label="User Reviews">
<h2>User Reviews</h2>
<ul class="reviews-list">
</ul>
</section>
<script src="/public/js/index.js" type="module"></script>
</body>
</html>

View file

@ -1,27 +1,27 @@
import { logger } from "@creations.works/logger";
import { echo } from "@atums/echo";
import type { ServerWebSocket } from "bun";
class WebSocketHandler {
public handleMessage(ws: ServerWebSocket, message: string): void {
logger.info(`WebSocket received: ${message}`);
echo.info(`WebSocket received: ${message}`);
try {
ws.send(`You said: ${message}`);
} catch (error) {
logger.error(["WebSocket send error", error as Error]);
echo.error({ message: "WebSocket send error", error: error });
}
}
public handleOpen(ws: ServerWebSocket): void {
logger.info("WebSocket connection opened.");
echo.info("WebSocket connection opened.");
try {
ws.send("Welcome to the WebSocket server!");
} catch (error) {
logger.error(["WebSocket send error", error as Error]);
echo.error({ message: "WebSocket send error", error: error });
}
}
public handleClose(ws: ServerWebSocket, code: number, reason: string): void {
logger.warn(`WebSocket closed with code ${code}, reason: ${reason}`);
echo.warn(`WebSocket closed with code ${code}, reason: ${reason}`);
}
}