|
| 1 | +import os |
1 | 2 | import uvicorn |
2 | 3 | from fastapi import FastAPI |
3 | 4 | from fastapi.middleware.cors import CORSMiddleware |
4 | 5 | from routes import query, azure, system, user_queries, data_documents |
5 | 6 |
|
6 | 7 | app = FastAPI() |
7 | 8 |
|
| 9 | +# Configure CORS origins based on environment |
| 10 | +# Check for production indicators |
| 11 | +is_production = ( |
| 12 | + os.getenv("ENVIRONMENT") == "production" |
| 13 | + or os.getenv("K_SERVICE") is not None # Google Cloud Run |
| 14 | +) |
| 15 | + |
| 16 | +if is_production: |
| 17 | + # Production: Only allow specific origins |
| 18 | + allowed_origins = [ |
| 19 | + "https://querypal.virtonomy.io", # Production frontend |
| 20 | + "https://querypal-frontend-zynyyoxona-ew.a.run.app", # Cloud Run frontend URL (pattern) |
| 21 | + # Add your actual Cloud Run frontend URL when you know it |
| 22 | + ] |
| 23 | +else: |
| 24 | + # Development: Allow localhost origins |
| 25 | + allowed_origins = [ |
| 26 | + "http://localhost:8000", |
| 27 | + "http://localhost:5173", |
| 28 | + "http://127.0.0.1:8000", |
| 29 | + "http://127.0.0.1:5173", |
| 30 | + ] |
| 31 | + |
| 32 | +print(f"🔧 CORS Configuration - Production mode: {is_production}") |
| 33 | +print(f"🌐 Allowed origins: {allowed_origins}") |
| 34 | + |
8 | 35 | app.add_middleware( |
9 | 36 | CORSMiddleware, |
10 | | - allow_origins=["*"], |
| 37 | + allow_origins=allowed_origins, |
11 | 38 | allow_credentials=True, |
12 | | - allow_methods=["*"], |
| 39 | + allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], |
13 | 40 | allow_headers=["*"], |
14 | 41 | ) |
15 | 42 |
|
| 43 | + |
| 44 | +@app.get("/health") |
| 45 | +async def health_check(): |
| 46 | + """Health check endpoint.""" |
| 47 | + return { |
| 48 | + "status": "healthy", |
| 49 | + "cors_production_mode": is_production, |
| 50 | + } |
| 51 | + |
| 52 | + |
16 | 53 | app.include_router(query.router, prefix="/query", tags=["Query"]) |
17 | 54 | app.include_router(azure.router, prefix="/azure", tags=["Azure"]) |
18 | 55 | app.include_router(system.router, prefix="/system", tags=["System"]) |
|
0 commit comments