Enhance backend configuration and error handling: Update CORS settings to allow dynamic origins, improve RabbitMQ connection handling in chat services, and adjust API server host configuration. Refactor environment variables for better flexibility and add fallback mechanisms for WebSocket and chat services. Update frontend environment files for consistent API and WebSocket URLs.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { getChatWsUrlFromEnv } from '@/utils/appConfig.js';
|
||||
|
||||
// Small helper to resolve the Chat WebSocket URL from env or sensible defaults
|
||||
export function getChatWsUrl() {
|
||||
// Prefer explicit env var
|
||||
@@ -5,24 +7,7 @@ export function getChatWsUrl() {
|
||||
if (override && typeof override === 'string' && override.trim()) {
|
||||
return override.trim();
|
||||
}
|
||||
const envUrl = import.meta?.env?.VITE_CHAT_WS_URL;
|
||||
if (envUrl && typeof envUrl === 'string' && envUrl.trim()) {
|
||||
return envUrl.trim();
|
||||
}
|
||||
// Fallback: use current origin host with ws/wss and default port/path if provided by backend
|
||||
const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:';
|
||||
const proto = isHttps ? 'wss' : 'ws';
|
||||
// If a reverse proxy exposes the chat at a path, you can change '/chat' here.
|
||||
const host = typeof window !== 'undefined' ? window.location.hostname : 'localhost';
|
||||
const port = (typeof window !== 'undefined' && window.location.port) ? `:${window.location.port}` : '';
|
||||
// On localhost, prefer dedicated chat port 1235 by default
|
||||
// Prefer IPv4 for localhost to avoid browsers resolving to ::1 (IPv6) where the server may not listen
|
||||
if (host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]') {
|
||||
return `${proto}://127.0.0.1:1235`;
|
||||
}
|
||||
// Default to same origin with chat port for production
|
||||
const defaultUrl = `${proto}://${host}:1235`;
|
||||
return defaultUrl;
|
||||
return getChatWsUrlFromEnv();
|
||||
}
|
||||
|
||||
// Provide a list of candidate WS URLs to try, in order of likelihood.
|
||||
@@ -31,37 +16,8 @@ export function getChatWsCandidates() {
|
||||
if (override && typeof override === 'string' && override.trim()) {
|
||||
return [override.trim()];
|
||||
}
|
||||
const envUrl = import.meta?.env?.VITE_CHAT_WS_URL;
|
||||
if (envUrl && typeof envUrl === 'string' && envUrl.trim()) {
|
||||
return [envUrl.trim()];
|
||||
}
|
||||
const isHttps = typeof window !== 'undefined' && window.location.protocol === 'https:';
|
||||
const proto = isHttps ? 'wss' : 'ws';
|
||||
const host = typeof window !== 'undefined' ? window.location.hostname : 'localhost';
|
||||
const port = (typeof window !== 'undefined' && window.location.port) ? `:${window.location.port}` : '';
|
||||
const candidates = [];
|
||||
// Common local setups: include IPv4 and IPv6 loopback variants (root only)
|
||||
if (host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]') {
|
||||
// Prefer IPv6 loopback first when available
|
||||
const localHosts = ['[::1]', '127.0.0.1', 'localhost'];
|
||||
for (const h of localHosts) {
|
||||
const base = `${proto}://${h}:1235`;
|
||||
candidates.push(base);
|
||||
candidates.push(`${base}/`);
|
||||
}
|
||||
}
|
||||
// Same-origin with chat port
|
||||
const sameOriginBases = [`${proto}://${host}:1235`];
|
||||
// If localhost-ish, also try 127.0.0.1 for chat port
|
||||
if (host === 'localhost' || host === '::1' || host === '[::1]') {
|
||||
sameOriginBases.push(`${proto}://[::1]:1235`);
|
||||
sameOriginBases.push(`${proto}://127.0.0.1:1235`);
|
||||
}
|
||||
for (const base of sameOriginBases) {
|
||||
candidates.push(base);
|
||||
candidates.push(`${base}/`);
|
||||
}
|
||||
return candidates;
|
||||
const resolved = getChatWsUrlFromEnv();
|
||||
return [resolved, `${resolved}/`];
|
||||
}
|
||||
|
||||
// Return optional subprotocols for the WebSocket handshake.
|
||||
@@ -84,4 +40,4 @@ export function getChatWsProtocols() {
|
||||
// Default to the 'chat' subprotocol so the server can gate connections accordingly
|
||||
return ['chat'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user