add route logging

This commit is contained in:
creations 2025-05-28 10:39:10 -04:00
parent 78910f0b66
commit 4483404e8b
Signed by: creations
GPG key ID: 8F553AA4320FC711

View file

@ -4,6 +4,8 @@ import { environment, verifyRequiredVariables } from "@config";
import { withCors } from "@lib/cors";
import { serve, sql } from "bun";
const ignoreRoutes = ["/favicon.ico"];
verifyRequiredVariables();
try {
@ -42,7 +44,34 @@ const auth = new DiscordAuth();
serve({
port: environment.port,
fetch: async (req) => {
fetch: async (req, server) => {
const url = new URL(req.url);
if (ignoreRoutes.includes(url.pathname)) {
return new Response(null, { status: 204 });
}
let ip = server.requestIP(req)?.address;
if (
!ip ||
ip.startsWith("172.") ||
ip === "127.0.0.1" ||
ip.startsWith("::ff")
) {
ip =
req.headers.get("CF-Connecting-IP")?.trim() ||
req.headers.get("X-Real-IP")?.trim() ||
req.headers.get("X-Forwarded-For")?.split(",")[0].trim() ||
"unknown";
}
echo.custom(req.method, `${url.pathname}${url.search}`, {
IP: ip,
"User-Agent": req.headers.get("User-Agent") || "unknown",
Referer: req.headers.get("Referer") || "unknown",
Origin: req.headers.get("Origin") || "unknown",
});
if (req.method === "OPTIONS") {
const origin = req.headers.get("origin") ?? "";
return new Response(null, {
@ -58,8 +87,6 @@ serve({
});
}
const url = new URL(req.url);
if (url.pathname === "/auth/discord") return auth.startOAuthRedirect(req);
if (url.pathname === "/auth/discord/callback")
return auth.handleCallback(req);