feat: forward /api/v1/post to /post in judd
This commit is contained in:
parent
bcbdc92f53
commit
3b40d565a4
2 changed files with 44 additions and 29 deletions
2
judd/package-lock.json
generated
2
judd/package-lock.json
generated
|
|
@ -616,6 +616,7 @@
|
|||
"resolved": "https://registry.npmjs.org/express/-/express-4.22.1.tgz",
|
||||
"integrity": "sha512-F2X8g9P1X7uCPZMA3MVf9wcTqlyNp7IhH5qPCI0izhaOIYXaW9L535tGA3qmjRzpH+bZczqq7hVKxTR4NWnu+g==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"accepts": "~1.3.8",
|
||||
"array-flatten": "1.1.1",
|
||||
|
|
@ -1142,6 +1143,7 @@
|
|||
"resolved": "https://registry.npmjs.org/pg/-/pg-8.20.0.tgz",
|
||||
"integrity": "sha512-ldhMxz2r8fl/6QkXnBD3CR9/xg694oT6DZQ2s6c/RI28OjtSOpxnPrUCGOBJ46RCUxcWdx3p6kw/xnDHjKvaRA==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"pg-connection-string": "^2.12.0",
|
||||
"pg-pool": "^3.13.0",
|
||||
|
|
|
|||
71
main.py
71
main.py
|
|
@ -119,36 +119,49 @@ app.add_middleware(
|
|||
|
||||
@app.middleware("http")
|
||||
async def proxy_fallback(request: Request, call_next):
|
||||
response = await call_next(request)
|
||||
path = request.url.path
|
||||
if path == "/api/v1/post":
|
||||
path = "/post"
|
||||
url = f"http://127.0.0.1:{settings.judd_port}{path}"
|
||||
body = await request.body()
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
judd_resp = await client.request(
|
||||
request.method,
|
||||
url,
|
||||
headers={k: v for k, v in request.headers.items() if k.lower() != "host"},
|
||||
params=request.query_params,
|
||||
content=body,
|
||||
)
|
||||
response = Response(
|
||||
content=judd_resp.content,
|
||||
status_code=judd_resp.status_code,
|
||||
headers=dict(judd_resp.headers),
|
||||
)
|
||||
except httpx.RequestError:
|
||||
response = await call_next(request)
|
||||
else:
|
||||
response = await call_next(request)
|
||||
if response.status_code == 404:
|
||||
url = f"http://127.0.0.1:{settings.judd_port}{path}"
|
||||
body = await request.body()
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
judd_resp = await client.request(
|
||||
request.method,
|
||||
url,
|
||||
headers={k: v for k, v in request.headers.items() if k.lower() != "host"},
|
||||
params=request.query_params,
|
||||
content=body,
|
||||
)
|
||||
response = Response(
|
||||
content=judd_resp.content,
|
||||
status_code=judd_resp.status_code,
|
||||
headers=dict(judd_resp.headers),
|
||||
)
|
||||
except httpx.RequestError:
|
||||
pass
|
||||
|
||||
if response.status_code != 404:
|
||||
return response
|
||||
|
||||
url = f"http://127.0.0.1:{settings.judd_port}{request.url.path}"
|
||||
body = await request.body()
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
judd_resp = await client.request(
|
||||
request.method,
|
||||
url,
|
||||
headers={k: v for k, v in request.headers.items() if k.lower() != "host"},
|
||||
params=request.query_params,
|
||||
content=body,
|
||||
)
|
||||
except httpx.RequestError:
|
||||
return response
|
||||
|
||||
return Response(
|
||||
content=judd_resp.content,
|
||||
status_code=judd_resp.status_code,
|
||||
headers=dict(judd_resp.headers),
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def force_cors_on_errors(request: Request, call_next):
|
||||
response = await call_next(request)
|
||||
origin = request.headers.get("origin")
|
||||
if origin == settings.frontend_url:
|
||||
response.headers["Access-Control-Allow-Origin"] = origin
|
||||
|
|
|
|||
Loading…
Reference in a new issue