Files
bakery-ia/gateway/app/routes/data.py

75 lines
3.2 KiB
Python
Raw Normal View History

2025-07-18 12:57:13 +02:00
"""Data service routes for API Gateway - Authentication handled by gateway middleware"""
2025-07-17 19:46:41 +02:00
2025-07-18 12:57:13 +02:00
from fastapi import APIRouter, Request, HTTPException
2025-07-18 11:51:43 +02:00
from fastapi.responses import StreamingResponse
2025-07-17 19:46:41 +02:00
import httpx
2025-07-18 12:57:13 +02:00
import logging
2025-07-17 19:46:41 +02:00
from app.core.config import settings
2025-07-18 12:57:13 +02:00
logger = logging.getLogger(__name__)
2025-07-17 19:46:41 +02:00
router = APIRouter()
2025-07-18 11:51:43 +02:00
@router.api_route("/sales/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
2025-07-18 12:57:13 +02:00
async def proxy_sales(request: Request, path: str):
2025-07-18 11:51:43 +02:00
"""Proxy sales data requests to data service"""
return await _proxy_request(request, f"/api/v1/sales/{path}")
2025-07-17 19:46:41 +02:00
2025-07-18 11:51:43 +02:00
@router.api_route("/weather/{path:path}", methods=["GET", "POST"])
2025-07-18 12:57:13 +02:00
async def proxy_weather(request: Request, path: str):
2025-07-18 11:51:43 +02:00
"""Proxy weather requests to data service"""
return await _proxy_request(request, f"/api/v1/weather/{path}")
@router.api_route("/traffic/{path:path}", methods=["GET", "POST"])
2025-07-18 12:57:13 +02:00
async def proxy_traffic(request: Request, path: str):
2025-07-18 11:51:43 +02:00
"""Proxy traffic requests to data service"""
return await _proxy_request(request, f"/api/v1/traffic/{path}")
async def _proxy_request(request: Request, target_path: str):
2025-07-18 12:57:13 +02:00
"""Proxy request to data service with user context"""
2025-07-17 19:46:41 +02:00
try:
2025-07-18 11:51:43 +02:00
url = f"{settings.DATA_SERVICE_URL}{target_path}"
2025-07-17 19:46:41 +02:00
2025-07-18 12:57:13 +02:00
# Forward headers BUT add user context from gateway auth
2025-07-18 11:51:43 +02:00
headers = dict(request.headers)
headers.pop("host", None) # Remove host header
2025-07-18 12:57:13 +02:00
# ✅ ADD USER CONTEXT FROM GATEWAY AUTHENTICATION
# Gateway middleware already verified the token and added user to request.state
if hasattr(request.state, 'user'):
headers["X-User-ID"] = str(request.state.user.get("user_id"))
headers["X-User-Email"] = request.state.user.get("email", "")
headers["X-Tenant-ID"] = str(request.state.user.get("tenant_id"))
headers["X-User-Roles"] = ",".join(request.state.user.get("roles", []))
2025-07-18 11:51:43 +02:00
# Get request body if present
body = None
if request.method in ["POST", "PUT", "PATCH"]:
body = await request.body()
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.request(
method=request.method,
url=url,
params=request.query_params,
headers=headers,
content=body
2025-07-17 19:46:41 +02:00
)
2025-07-18 11:51:43 +02:00
# Return streaming response for large payloads
if int(response.headers.get("content-length", 0)) > 1024:
return StreamingResponse(
iter([response.content]),
status_code=response.status_code,
headers=dict(response.headers),
media_type=response.headers.get("content-type")
)
else:
return response.json() if response.headers.get("content-type", "").startswith("application/json") else response.content
2025-07-17 19:46:41 +02:00
except httpx.RequestError as e:
2025-07-18 11:51:43 +02:00
logger.error("Data service request failed", error=str(e))
raise HTTPException(status_code=503, detail="Data service unavailable")
except Exception as e:
logger.error("Unexpected error in data proxy", error=str(e))
raise HTTPException(status_code=500, detail="Internal server error")