feat: añadir logs y endpoint de salud en proxy y login

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
webui
brobert 2 weeks ago
parent cdfd16f2fa
commit b7c8e37a85

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

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

Loading…
Cancel
Save