Fix empty Language and AI Model dropdowns
Browse files- Update CORS configuration to allow HuggingFace Space domain and use regex for *.hf.space
- Improve API URL logic in frontend to correctly detect production vs dev environment
- Fix EventSource and WebSocket URL construction to work in both environments
- Allow requests from localhost:7860 for Docker Space internal communication
Files changed (2)
- backend_api.py+11-2
- frontend/src/lib/api.ts+37-15
33 unmodified lines3435363738394041424344454633 unmodified linesapp = FastAPI(title="AnyCoder API", version="1.0.0")# Configure CORSapp.add_middleware(CORSMiddleware,allow_origins=["http://localhost:3000", "http://localhost:3001"], # Frontend URLsallow_credentials=True,allow_methods=["*"],allow_headers=["*"],)# OAuth configuration33 unmodified lines3435363738394041424344454647484950515253545533 unmodified linesapp = FastAPI(title="AnyCoder API", version="1.0.0")# Configure CORS - allow all origins in production, specific in dev# In Docker Space, requests come from the same domain via Next.js proxyALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "*").split(",") if os.getenv("ALLOWED_ORIGINS") else ["http://localhost:3000","http://localhost:3001","http://localhost:7860",f"https://{SPACE_HOST}" if SPACE_HOST and not SPACE_HOST.startswith("localhost") else "http://localhost:7860"]app.add_middleware(CORSMiddleware,allow_origins=ALLOWED_ORIGINS if ALLOWED_ORIGINS != ["*"] else ["*"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],allow_origin_regex=r"https://.*\.hf\.space" if os.getenv("SPACE_HOST") else None,)# OAuth configuration
10 unmodified lines111213141516171819202160 unmodified lines82838485868788899091929394959632 unmodified lines12913013113213313413510 unmodified lines} from '@/types';// Use relative URLs in production (Next.js rewrites will proxy to backend)// In local dev without rewrites, use localhost:8000const API_URL = process.env.NEXT_PUBLIC_API_URL ||(typeof window !== 'undefined' && window.location.hostname !== 'localhost'? '' // Use relative URLs in production (proxied by Next.js): 'http://localhost:8000'); // Local developmentclass ApiClient {private client: AxiosInstance;60 unmodified linesonComplete: (code: string) => void,onError: (error: string) => void): () => void {const url = new URL('/api/generate', API_URL);const eventSource = new EventSource(url.toString() + '?' + new URLSearchParams({query: request.query,language: request.language,model_id: request.model_id,provider: request.provider,}));eventSource.onmessage = (event) => {try {32 unmodified linesonComplete: (code: string) => void,onError: (error: string) => void): WebSocket {const wsUrl = API_URL.replace('http', 'ws') + '/ws/generate';const ws = new WebSocket(wsUrl);ws.onopen = () => {10 unmodified lines111213141516171819202122232425262728293031323334353637383960 unmodified lines10010110210310410510610710810911011111211311411511632 unmodified lines14915015115215315415515615710 unmodified lines} from '@/types';// Use relative URLs in production (Next.js rewrites will proxy to backend)// In local dev, use localhost:8000 for direct backend accessconst getApiUrl = () => {// If explicitly set via env var, use itif (process.env.NEXT_PUBLIC_API_URL) {return process.env.NEXT_PUBLIC_API_URL;}// For server-side rendering, always use relative URLsif (typeof window === 'undefined') {return '';}// On localhost (dev mode), use direct backend URLconst hostname = window.location.hostname;if (hostname === 'localhost' || hostname === '127.0.0.1') {return 'http://localhost:8000';}// In production (HF Space), use relative URLs (Next.js proxies to backend)return '';};const API_URL = getApiUrl();class ApiClient {private client: AxiosInstance;60 unmodified linesonComplete: (code: string) => void,onError: (error: string) => void): () => void {// Build the URL correctly whether we have a base URL or notconst baseUrl = API_URL || window.location.origin;const url = new URL('/api/generate', baseUrl);url.search = new URLSearchParams({query: request.query,language: request.language,model_id: request.model_id,provider: request.provider,}).toString();const eventSource = new EventSource(url.toString());eventSource.onmessage = (event) => {try {32 unmodified linesonComplete: (code: string) => void,onError: (error: string) => void): WebSocket {// Build WebSocket URL correctly for both dev and productionconst baseUrl = API_URL || window.location.origin;const wsUrl = baseUrl.replace('http', 'ws') + '/ws/generate';const ws = new WebSocket(wsUrl);ws.onopen = () => {
backend_api.py+11-2
frontend/src/lib/api.ts+37-15