fix(server): bootstrap HTTP server immediately and reconnect MongoDB in background to eliminate Bad Gateway

This commit is contained in:
AI Bot 2026-10-13 19:38:41 -05:00
parent b38157b94a
commit 926b5c5fce

View File

@ -282,10 +282,23 @@ const server = http.createServer(async (req, res) => {
});
async function bootstrap() {
await connectDatabase();
// 1. Iniciar servidor HTTP de inmediato para responder 200 OK a Traefik y visitantes
server.listen(PORT, () => {
console.log(`🚀 [Servidor Transporte Cali - Buenaventura] Ejecutándose en puerto ${PORT}`);
console.log(`🚀 [Pacifictrans Go] Plataforma Web y API escuchando en puerto ${PORT}`);
});
// 2. Conectar a MongoDB de forma no bloqueante con reintento automático
const tryConnect = async () => {
try {
await connectDatabase();
console.log('✅ [MongoDB] Conectado exitosamente en segundo plano.');
} catch (err: any) {
console.warn(`⚠️ [MongoDB Warning] Conexión no disponible (${err.message || 'ENOTFOUND'}). Reintentando en 10s...`);
setTimeout(tryConnect, 10000);
}
};
tryConnect();
}
bootstrap().catch(console.error);