Skip to content

Commit d607fc9

Browse files
authored
Merge pull request #16 from ChingEnLin/fix/cors
Fix/cors
2 parents f79a69b + 7a66b93 commit d607fc9

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-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: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,55 @@
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"
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+
835
app.add_middleware(
936
CORSMiddleware,
10-
allow_origins=["*"],
37+
allow_origins=allowed_origins,
1138
allow_credentials=True,
12-
allow_methods=["*"],
39+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
1340
allow_headers=["*"],
1441
)
1542

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+
1653
app.include_router(query.router, prefix="/query", tags=["Query"])
1754
app.include_router(azure.router, prefix="/azure", tags=["Azure"])
1855
app.include_router(system.router, prefix="/system", tags=["System"])

0 commit comments

Comments
 (0)