Spaces:
Paused
Paused
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Header, HTTPException
|
| 2 |
+
import httpx
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
|
| 6 |
+
# Replace with your actual backend API key and base URL
|
| 7 |
+
API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
|
| 8 |
+
BASE_URL = "https://fast.typegpt.net"
|
| 9 |
+
|
| 10 |
+
# Custom public header
|
| 11 |
+
PUBLIC_API_KEY = "TypeGPT-Free4ALL"
|
| 12 |
+
|
| 13 |
+
@app.api_route("/{path:path}", methods=["GET", "POST"])
|
| 14 |
+
async def proxy(request: Request, path: str, x_api_key: str = Header(None)):
|
| 15 |
+
if x_api_key != PUBLIC_API_KEY:
|
| 16 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 17 |
+
|
| 18 |
+
# Reconstruct full URL to target
|
| 19 |
+
target_url = f"{BASE_URL}/{path}"
|
| 20 |
+
|
| 21 |
+
# Prepare headers and body
|
| 22 |
+
headers = dict(request.headers)
|
| 23 |
+
headers["Authorization"] = f"Bearer {API_KEY}"
|
| 24 |
+
headers.pop("host", None)
|
| 25 |
+
|
| 26 |
+
body = await request.body()
|
| 27 |
+
|
| 28 |
+
async with httpx.AsyncClient() as client:
|
| 29 |
+
response = await client.request(
|
| 30 |
+
request.method,
|
| 31 |
+
target_url,
|
| 32 |
+
content=body,
|
| 33 |
+
headers=headers
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
return response.json()
|