Added CORS headers

This commit is contained in:
Dillon DuPont
2025-08-27 15:33:32 -04:00
parent cdc2b58d94
commit e0fb0235df
2 changed files with 84 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request, HTTPException, Header
from fastapi.responses import StreamingResponse
from fastapi.responses import StreamingResponse, JSONResponse
from typing import List, Dict, Any, Optional, Union, Literal, cast
import uvicorn
import logging
@@ -650,13 +650,68 @@ async def agent_response_endpoint(
if turns > 2:
break
return {
# Build response payload
payload = {
"success": True,
"model": model,
"output": total_output,
"usage": total_usage,
}
# Set CORS headers for allowed origins only
origin = request.headers.get("origin")
allowed_origins = {
"http://localhost",
"http://localhost:3000",
"http://localhost:5173",
"http://127.0.0.1",
"http://127.0.0.1:3000",
"https://trycua.com",
"https://www.trycua.com",
}
headers = {
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, X-Container-Name, X-API-Key",
}
if origin and origin in allowed_origins:
headers["Access-Control-Allow-Origin"] = origin
headers["Vary"] = "Origin"
return JSONResponse(content=payload, headers=headers)
@app.options("/responses")
async def agent_response_options(request: Request):
"""CORS preflight for /responses"""
origin = request.headers.get("origin")
allowed_origins = {
"http://localhost",
"http://localhost:3000",
"http://localhost:3001",
"http://localhost:5173",
"http://127.0.0.1",
"http://127.0.0.1:3000",
"http://127.0.0.1:3001",
"https://trycua.com",
"https://www.trycua.com",
}
headers = {
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, X-Container-Name, X-API-Key",
"Access-Control-Max-Age": "600",
}
if origin and origin in allowed_origins:
headers["Access-Control-Allow-Origin"] = origin
headers["Vary"] = "Origin"
return JSONResponse(content={}, headers=headers)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)