AIMaster7 commited on
Commit
5a86b8e
·
verified ·
1 Parent(s): d56017b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +18 -19
main.py CHANGED
@@ -3,39 +3,38 @@ 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
- # Public API key for users to use
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
- # Check if the public API key matches the expected value
16
- if x_api_key != PUBLIC_API_KEY:
17
- raise HTTPException(status_code=401, detail="Invalid API key. Please use 'TypeGPT-Free4ALL'.")
18
 
19
- # Reconstruct full URL to target endpoint
20
  target_url = f"{BASE_URL}/{path}"
21
 
22
- # Prepare headers from the incoming request
23
  headers = dict(request.headers)
24
- headers["Authorization"] = f"Bearer {API_KEY}" # Add your secret API key here
25
- headers.pop("host", None) # Remove host header to avoid conflicts
26
 
27
- # Get the body of the request if present
28
  body = await request.body()
29
 
30
- # Make the actual request to the backend API
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 the response back to the user
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()