Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,39 +3,38 @@ import httpx
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
BASE_URL = "https://fast.typegpt.net"
|
| 9 |
|
| 10 |
-
# Public API key
|
| 11 |
-
|
| 12 |
|
| 13 |
@app.api_route("/{path:path}", methods=["GET", "POST"])
|
| 14 |
-
async def proxy(request: Request, path: str,
|
| 15 |
-
#
|
| 16 |
-
if
|
| 17 |
-
raise HTTPException(status_code=401, detail="Invalid
|
| 18 |
|
| 19 |
-
#
|
| 20 |
target_url = f"{BASE_URL}/{path}"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
headers = dict(request.headers)
|
| 24 |
-
headers["Authorization"] = f"Bearer {
|
| 25 |
-
headers.pop("host", None) #
|
| 26 |
|
| 27 |
-
# Get the
|
| 28 |
body = await request.body()
|
| 29 |
|
| 30 |
-
#
|
| 31 |
async with httpx.AsyncClient() as client:
|
| 32 |
response = await client.request(
|
| 33 |
-
request.method,
|
| 34 |
-
target_url,
|
| 35 |
content=body,
|
| 36 |
-
headers=headers
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# Return
|
| 40 |
return response.json()
|
| 41 |
-
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
+
# Internal secret key (never exposed to users)
|
| 7 |
+
REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
|
| 8 |
BASE_URL = "https://fast.typegpt.net"
|
| 9 |
|
| 10 |
+
# Public API key that users must send in the Authorization header
|
| 11 |
+
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
|
| 12 |
|
| 13 |
@app.api_route("/{path:path}", methods=["GET", "POST"])
|
| 14 |
+
async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
| 15 |
+
# Validate public access token
|
| 16 |
+
if authorization != PUBLIC_AUTH_TOKEN:
|
| 17 |
+
raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
|
| 18 |
|
| 19 |
+
# Construct the target URL
|
| 20 |
target_url = f"{BASE_URL}/{path}"
|
| 21 |
|
| 22 |
+
# Copy headers from request and replace Authorization with real key
|
| 23 |
headers = dict(request.headers)
|
| 24 |
+
headers["Authorization"] = f"Bearer {REAL_API_KEY}"
|
| 25 |
+
headers.pop("host", None) # remove 'host' header if present
|
| 26 |
|
| 27 |
+
# Get the request body
|
| 28 |
body = await request.body()
|
| 29 |
|
| 30 |
+
# Send request to backend (TypeGPT)
|
| 31 |
async with httpx.AsyncClient() as client:
|
| 32 |
response = await client.request(
|
| 33 |
+
method=request.method,
|
| 34 |
+
url=target_url,
|
| 35 |
content=body,
|
| 36 |
+
headers=headers,
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Return JSON response from TypeGPT
|
| 40 |
return response.json()
|
|
|