File size: 1,765 Bytes
1a12d36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * 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;