Commit
·
6ace0b8
1
Parent(s):
c6bb0a1
New approach part 4
Browse files
app.py
CHANGED
@@ -141,21 +141,24 @@
|
|
141 |
|
142 |
from fastapi import FastAPI, Request
|
143 |
import httpx
|
|
|
|
|
|
|
|
|
144 |
|
145 |
app = FastAPI()
|
146 |
TARGET_SERVER = "http://localhost:11434"
|
147 |
|
148 |
-
@app.get("/{path:path}")
|
149 |
async def proxy_get(path: str):
|
150 |
""" Forwards GET requests to the target server """
|
151 |
async with httpx.AsyncClient() as client:
|
152 |
response = await client.get(f"{TARGET_SERVER}/{path}")
|
153 |
return response.json()
|
154 |
|
155 |
-
@app.post("/{path:path}")
|
156 |
-
async def proxy_post(path: str,
|
157 |
""" Forwards POST requests to the target server """
|
158 |
-
data = await request.json() # Get the request body
|
159 |
async with httpx.AsyncClient() as client:
|
160 |
-
response = await client.post(f"{TARGET_SERVER}/{path}", json=
|
161 |
return response.json()
|
|
|
141 |
|
142 |
from fastapi import FastAPI, Request
|
143 |
import httpx
|
144 |
+
from pydantic import BaseModel
|
145 |
+
|
146 |
+
class RequestBody(BaseModel):
|
147 |
+
data: dict
|
148 |
|
149 |
app = FastAPI()
|
150 |
TARGET_SERVER = "http://localhost:11434"
|
151 |
|
152 |
+
@app.get("/proxy/{path:path}")
|
153 |
async def proxy_get(path: str):
|
154 |
""" Forwards GET requests to the target server """
|
155 |
async with httpx.AsyncClient() as client:
|
156 |
response = await client.get(f"{TARGET_SERVER}/{path}")
|
157 |
return response.json()
|
158 |
|
159 |
+
@app.post("/proxy/{path:path}")
|
160 |
+
async def proxy_post(path: str, request_body: RequestBody):
|
161 |
""" Forwards POST requests to the target server """
|
|
|
162 |
async with httpx.AsyncClient() as client:
|
163 |
+
response = await client.post(f"{TARGET_SERVER}/{path}", json = request_body.model_dump())
|
164 |
return response.json()
|