Spaces:
Runtime error
Runtime error
| /** | |
| * API Configuration - Backend URL Settings | |
| * | |
| * This file configures the backend API URL for separate frontend/backend hosting. | |
| * | |
| * Configuration priority: | |
| * 1. Environment variable VITE_BACKEND_URL (set in .env file) | |
| * 2. window.BACKEND_URL (set in index.html via global variable) | |
| * 3. Default: http://localhost:7860 (local development) | |
| */ | |
| // Get backend URL from environment or global window object | |
| const getBackendUrl = (): string => { | |
| // Try environment variable first (Vite exposes env vars prefixed with VITE_) | |
| const envUrl = import.meta.env.VITE_BACKEND_URL; | |
| if (envUrl) { | |
| return envUrl; | |
| } | |
| // Try global window object (for runtime configuration) | |
| if (typeof window !== 'undefined' && (window as unknown as { BACKEND_URL?: string }).BACKEND_URL) { | |
| return (window as unknown as { BACKEND_URL: string }).BACKEND_URL; | |
| } | |
| // Default for local development | |
| return 'http://localhost:7860'; | |
| }; | |
| // Get WebSocket URL based on backend URL | |
| const getWebSocketBaseUrl = (): string => { | |
| const backendUrl = getBackendUrl(); | |
| const url = new URL(backendUrl); | |
| const protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'; | |
| return `${protocol}//${url.host}`; | |
| }; | |
| export const API_CONFIG = { | |
| backendUrl: getBackendUrl(), | |
| websocketBaseUrl: getWebSocketBaseUrl(), | |
| // API endpoints | |
| apiPrefix: '/api', | |
| authPrefix: '/auth', | |
| // Build full URLs | |
| getApiUrl: (path: string): string => { | |
| const base = getBackendUrl(); | |
| const cleanPath = path.startsWith('/') ? path : `/${path}`; | |
| return `${base}${cleanPath}`; | |
| }, | |
| // Build WebSocket URL | |
| getWebSocketUrl: (sessionId: string): string => { | |
| const base = getWebSocketBaseUrl(); | |
| return `${base}/api/ws/${sessionId}`; | |
| }, | |
| }; | |
| export default API_CONFIG; | |