| |
| let runtimeConfig: { |
| API_BASE_URL: string; |
| } | null = null; |
|
|
| |
| let configLoading = true; |
|
|
| |
| const defaultConfig = { |
| API_BASE_URL: 'http://127.0.0.1:8000', |
| }; |
|
|
| |
| export async function loadRuntimeConfig(): Promise<void> { |
| try { |
| console.log('π§ DEBUG: Starting to load runtime config...'); |
| |
| const response = await fetch('/api/config'); |
| if (response.ok) { |
| const contentType = response.headers.get('content-type'); |
| |
| if (contentType && contentType.includes('application/json')) { |
| runtimeConfig = await response.json(); |
| console.log('Runtime config loaded successfully'); |
| } else { |
| console.log( |
| 'Config endpoint returned non-JSON response, skipping runtime config' |
| ); |
| } |
| } else { |
| console.log( |
| 'π§ DEBUG: Config fetch failed with status:', |
| response.status |
| ); |
| } |
| } catch (error) { |
| console.log('Failed to load runtime config, using defaults:', error); |
| } finally { |
| configLoading = false; |
| console.log( |
| 'π§ DEBUG: Config loading finished, configLoading set to false' |
| ); |
| } |
| } |
|
|
| |
| export function getConfig() { |
| |
| if (configLoading) { |
| console.log('Config still loading, using default config'); |
| return defaultConfig; |
| } |
|
|
| |
| if (runtimeConfig) { |
| console.log('Using runtime config'); |
| return runtimeConfig; |
| } |
|
|
| |
| if (import.meta.env.VITE_API_BASE_URL) { |
| const viteConfig = { |
| API_BASE_URL: import.meta.env.VITE_API_BASE_URL, |
| }; |
| console.log('Using Vite environment config'); |
| return viteConfig; |
| } |
|
|
| |
| console.log('Using default config'); |
| return defaultConfig; |
| } |
|
|
| |
| export function getAPIBaseURL(): string { |
| const baseURL = getConfig().API_BASE_URL; |
| |
| if (baseURL === '/') { |
| return ''; |
| } |
| return baseURL; |
| } |
|
|
| |
| |
| |
|
|
| export const config = { |
| get API_BASE_URL() { |
| return getAPIBaseURL(); |
| }, |
| }; |
|
|