chatterbox-ui/frontend/js/config.js

64 lines
2.3 KiB
JavaScript

// Frontend Configuration
// This file handles environment variable configuration for the frontend
// Get environment variables (these would be injected by a build tool like Vite)
// For now, we'll use defaults that can be overridden
const getEnvVar = (name, defaultValue) => {
// In a real Vite setup, this would be import.meta.env[name]
// For now, we'll check if there's a global config object or use defaults
if (typeof window !== 'undefined' && window.APP_CONFIG && window.APP_CONFIG[name]) {
return window.APP_CONFIG[name];
}
return defaultValue;
};
// API Configuration - Dynamic backend detection
const DEFAULT_BACKEND_PORTS = [8000, 8001, 8002, 8003, 8004];
const AUTO_DETECT_BACKEND = getEnvVar('VITE_AUTO_DETECT_BACKEND', 'true') === 'true';
// Function to detect available backend
async function detectBackendUrl() {
if (!AUTO_DETECT_BACKEND) {
return getEnvVar('VITE_API_BASE_URL', 'http://localhost:8000');
}
for (const port of DEFAULT_BACKEND_PORTS) {
try {
const testUrl = `http://localhost:${port}`;
const response = await fetch(`${testUrl}/`, { method: 'GET', timeout: 1000 });
if (response.ok) {
console.log(`✅ Detected backend at ${testUrl}`);
return testUrl;
}
} catch (e) {
// Port not available, try next
}
}
// Fallback to default
console.warn('⚠️ Could not detect backend, using default http://localhost:8000');
return 'http://localhost:8000';
}
// For now, use the configured values (detection can be added later if needed)
export const API_BASE_URL = getEnvVar('VITE_API_BASE_URL', 'http://localhost:8000');
export const API_BASE_URL_WITH_PREFIX = getEnvVar('VITE_API_BASE_URL_WITH_PREFIX', 'http://localhost:8000/api');
// For file serving (same as API_BASE_URL since files are served from the same server)
export const API_BASE_URL_FOR_FILES = API_BASE_URL;
// Development server configuration
export const DEV_SERVER_PORT = getEnvVar('VITE_DEV_SERVER_PORT', '8001');
export const DEV_SERVER_HOST = getEnvVar('VITE_DEV_SERVER_HOST', '127.0.0.1');
// Export all config as a single object for convenience
export const CONFIG = {
API_BASE_URL,
API_BASE_URL_WITH_PREFIX,
API_BASE_URL_FOR_FILES,
DEV_SERVER_PORT,
DEV_SERVER_HOST
};
export default CONFIG;