From b7c8e37a8541a7cd16093fe92dc428a397410d7b Mon Sep 17 00:00:00 2001 From: brobert Date: Sun, 12 Oct 2025 23:47:16 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20a=C3=B1adir=20logs=20y=20endpoint=20de?= =?UTF-8?q?=20salud=20en=20proxy=20y=20login?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/openai/gpt-5) --- apps/web/src/routes/login/+server.ts | 2 ++ proxy.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/apps/web/src/routes/login/+server.ts b/apps/web/src/routes/login/+server.ts index b5c82b3..78a3bc4 100644 --- a/apps/web/src/routes/login/+server.ts +++ b/apps/web/src/routes/login/+server.ts @@ -11,6 +11,7 @@ function toIsoSql(d: Date): string { export const GET: RequestHandler = async (event) => { const token = event.url.searchParams.get('token')?.trim(); if (!token) { + console.warn('[web/login] Solicitud sin token'); return new Response('Falta el token', { status: 400 }); } @@ -32,6 +33,7 @@ export const GET: RequestHandler = async (event) => { const changes = Number(res?.changes || 0); if (changes < 1) { + console.warn('[web/login] Token no canjeado (0 cambios). Posible caducado/ya usado/no existe.'); return new Response('Enlace inválido o caducado', { status: 400 }); } diff --git a/proxy.ts b/proxy.ts index 1b2fa39..42e58d2 100644 --- a/proxy.ts +++ b/proxy.ts @@ -14,6 +14,8 @@ function buildForwardHeaders(req: Request): Headers { const fwdFor = headers.get('x-forwarded-for'); headers.set('x-forwarded-proto', proto); headers.set('x-forwarded-for', fwdFor ? `${fwdFor}, 127.0.0.1` : '127.0.0.1'); + const host = headers.get('host') || ''; + if (!host) headers.set('host', 'localhost'); } catch {} return headers; } @@ -22,7 +24,14 @@ Bun.serve({ port: Number(process.env.PORT || process.env.ROUTER_PORT || 3000), fetch: async (req) => { const url = new URL(req.url); - const targetOrigin = shouldRouteToBot(url.pathname) ? BOT_ORIGIN : WEB_ORIGIN; + + // Health local para el contenedor (evita 404 en healthcheck) + if (url.pathname === '/health') { + return new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } }); + } + + const routeToBot = shouldRouteToBot(url.pathname); + const targetOrigin = routeToBot ? BOT_ORIGIN : WEB_ORIGIN; const targetUrl = targetOrigin + url.pathname + url.search; const init: RequestInit = { @@ -32,12 +41,18 @@ Bun.serve({ redirect: 'manual', }; + const started = Date.now(); try { const res = await fetch(targetUrl, init); + const ms = Date.now() - started; + try { + console.log(`[proxy] ${req.method} ${url.pathname}${url.search} -> ${routeToBot ? 'bot' : 'web'} ${res.status} (${ms}ms)`); + } catch {} // Devuelve la respuesta tal cual (incluye Set-Cookie, Location, etc.) return res; } catch (err) { const msg = err instanceof Error ? err.message : String(err); + console.error(`[proxy] ${req.method} ${url.pathname}${url.search} -> ERROR: ${msg}`); return new Response(`Proxy error: ${msg}\n`, { status: 502 }); } },