Adilmar commited on
Commit
4c57e3f
·
verified ·
1 Parent(s): 46f50e9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, Response
2
+ import httpx
3
+ import os
4
+
5
+ app = FastAPI()
6
+
7
+ BACKEND_URL = os.environ.get("BACKEND_URL")
8
+ AUTH_HEADER = os.environ.get("AUTH_HEADER")
9
+
10
+ @app.api_route("/{full_path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
11
+ async def proxy(full_path: str, request: Request):
12
+ # Monta URL destino
13
+ url = f"{BACKEND_URL}/{full_path}"
14
+
15
+ # Copia headers originais e adiciona Authorization
16
+ headers = dict(request.headers)
17
+ headers["Authorization"] = AUTH_HEADER
18
+
19
+ # Lê corpo da requisição
20
+ body = await request.body()
21
+
22
+ # Faz requisição ao backend
23
+ async with httpx.AsyncClient() as client:
24
+ resp = await client.request(
25
+ method=request.method,
26
+ url=url,
27
+ headers=headers,
28
+ content=body,
29
+ params=dict(request.query_params)
30
+ )
31
+
32
+ # Retorna resposta do backend
33
+ return Response(
34
+ content=resp.content,
35
+ status_code=resp.status_code,
36
+ headers=dict(resp.headers)
37
+ )
38
+
39
+ # Para rodar:
40
+ # uvicorn proxy:app --reload --port 8000