-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_app.py
More file actions
29 lines (24 loc) · 1.17 KB
/
proxy_app.py
File metadata and controls
29 lines (24 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# proxy_app.py
import os, httpx
from fastapi import FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware
GATEWAY = os.getenv("GATEWAY_BASE", "https://targetval-gateway.onrender.com")
API_KEY = os.getenv("TARGETVAL_API_KEY", "")
app = FastAPI(title="TargetVal MCP Proxy")
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
@app.api_route("/http/{path:path}", methods=["GET","POST","PUT","PATCH","DELETE","OPTIONS","HEAD"])
async def http_passthrough(path: str, request: Request):
url = f"{GATEWAY.rstrip('/')}/{path}"
qs = request.url.query
if qs:
url = f"{url}?{qs}"
body = await request.body() if request.method in ("POST","PUT","PATCH") else None
headers = dict(request.headers)
headers["host"] = ""
if API_KEY:
headers["x-api-key"] = API_KEY
headers["user-agent"] = "TargetVal-MCP/2.0"
timeout = httpx.Timeout(30.0, connect=10.0)
async with httpx.AsyncClient(timeout=timeout) as client:
r = await client.request(request.method, url, headers=headers, content=body)
return Response(content=r.content, status_code=r.status_code, headers=dict(r.headers))