Add DEMO feature to the project
This commit is contained in:
61
gateway/app/routes/demo.py
Normal file
61
gateway/app/routes/demo.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""
|
||||
Demo Session Routes - Proxy to demo-session service
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Request, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
import httpx
|
||||
import structlog
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.api_route("/demo/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
||||
async def proxy_demo_service(path: str, request: Request):
|
||||
"""
|
||||
Proxy all demo requests to the demo-session service
|
||||
These endpoints are public and don't require authentication
|
||||
"""
|
||||
# Build the target URL
|
||||
demo_service_url = settings.DEMO_SESSION_SERVICE_URL.rstrip('/')
|
||||
target_url = f"{demo_service_url}/api/demo/{path}"
|
||||
|
||||
# Get request body
|
||||
body = None
|
||||
if request.method in ["POST", "PUT", "PATCH"]:
|
||||
body = await request.body()
|
||||
|
||||
# Forward headers (excluding host)
|
||||
headers = {
|
||||
key: value
|
||||
for key, value in request.headers.items()
|
||||
if key.lower() not in ["host", "content-length"]
|
||||
}
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
response = await client.request(
|
||||
method=request.method,
|
||||
url=target_url,
|
||||
headers=headers,
|
||||
params=request.query_params,
|
||||
content=body
|
||||
)
|
||||
|
||||
# Return the response
|
||||
return JSONResponse(
|
||||
content=response.json() if response.content else {},
|
||||
status_code=response.status_code,
|
||||
headers=dict(response.headers)
|
||||
)
|
||||
|
||||
except httpx.RequestError as e:
|
||||
logger.error("Failed to proxy to demo-session service", error=str(e), url=target_url)
|
||||
raise HTTPException(status_code=503, detail="Demo service unavailable")
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error proxying to demo-session service", error=str(e))
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
Reference in New Issue
Block a user