Skip to content

Commit 08afe20

Browse files
committed
feat: Enhance CORS configuration for production and development environments
1 parent f79a69b commit 08afe20

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

.github/workflows/google-cloudrun-docker.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
region: '${{ env.REGION }}'
6464
image: 'gcr.io/${{ env.PROJECT_ID }}/${{ env.BACKEND_SERVICE }}:${{ github.sha }}'
6565
env_vars: |
66+
ENVIRONMENT=production
6667
AZURE_TENANT_ID=${{ secrets.AZURE_TENANT_ID }}
6768
AZURE_CLIENT_ID=${{ secrets.AZURE_CLIENT_ID }}
6869
AZURE_CLIENT_SECRET=${{ secrets.AZURE_CLIENT_SECRET }}

backend/main.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,58 @@
1+
import os
12
import uvicorn
23
from fastapi import FastAPI
34
from fastapi.middleware.cors import CORSMiddleware
45
from routes import query, azure, system, user_queries, data_documents
56

67
app = FastAPI()
78

9+
# Configure CORS origins based on environment
10+
# Check for production indicators
11+
is_production = (
12+
os.getenv("ENVIRONMENT") == "production" or
13+
os.getenv("GAE_APPLICATION") or # Google App Engine
14+
os.getenv("GOOGLE_CLOUD_PROJECT") or # Google Cloud
15+
os.getenv("K_SERVICE") # Google Cloud Run
16+
)
17+
18+
if is_production:
19+
# Production: Only allow specific origins
20+
allowed_origins = [
21+
"https://querypal.virtonomy.io", # Production frontend
22+
"https://querypal-frontend-zynyyoxona-ew.a.run.app", # Cloud Run frontend URL (pattern)
23+
# Add your actual Cloud Run frontend URL when you know it
24+
]
25+
else:
26+
# Development: Allow localhost origins
27+
allowed_origins = [
28+
"http://localhost:3000",
29+
"http://localhost:5173",
30+
"http://localhost:8080",
31+
"http://127.0.0.1:3000",
32+
"http://127.0.0.1:5173",
33+
"http://127.0.0.1:8080",
34+
]
35+
36+
print(f"🔧 CORS Configuration - Production mode: {is_production}")
37+
print(f"🌐 Allowed origins: {allowed_origins}")
38+
839
app.add_middleware(
940
CORSMiddleware,
10-
allow_origins=["*"],
41+
allow_origins=allowed_origins,
1142
allow_credentials=True,
12-
allow_methods=["*"],
43+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
1344
allow_headers=["*"],
1445
)
1546

47+
@app.get("/health")
48+
async def health_check():
49+
"""Health check endpoint that also shows CORS configuration."""
50+
return {
51+
"status": "healthy",
52+
"cors_production_mode": is_production,
53+
"cors_allowed_origins": allowed_origins
54+
}
55+
1656
app.include_router(query.router, prefix="/query", tags=["Query"])
1757
app.include_router(azure.router, prefix="/azure", tags=["Azure"])
1858
app.include_router(system.router, prefix="/system", tags=["System"])

0 commit comments

Comments
 (0)