File size: 1,314 Bytes
d1ce9aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e43332
 
 
 
 
9103b97
d1ce9aa
 
 
3e43332
d1ce9aa
 
 
 
3e43332
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
30
31
32
33
34
35
36
37
38
39
from fastapi import FastAPI, Request
from fastapi.responses import Response
import httpx

app = FastAPI()

@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
async def proxy(request: Request, path: str):
    target_url = f"http://www.medicalgpt.club/{path}"
    
    async with httpx.AsyncClient() as client:
        method = request.method
        headers = dict(request.headers)
        params = request.query_params
        content = await request.body()

        response = await client.request(
            method=method,
            url=target_url,
            headers=headers,
            params=params,
            content=content
        )

        # Process the Location header if present in the response
        response_headers = dict(response.headers)
        if "Location" in response_headers:
            # Replace the target URL with the proxy URL
            response_headers["Location"] = response_headers["Location"].replace("http://www.medicalgpt.club", request.base_url)

        return Response(
            content=response.content,
            status_code=response.status_code,
            headers=response_headers
        )

if __name__ == "__main__":
    import os
    os.system("uvicorn main:app --host 0.0.0.0 --port 7860")