|
|
|
@@ -132,15 +132,36 @@ app.add_middleware( |
|
|
|
allow_headers=["*"], |
|
|
|
) |
|
|
|
|
|
|
|
# app.py |
|
|
|
EXACT_PROXY_PATHS = { |
|
|
|
"/reslevis/postGateway", |
|
|
|
"/reslevis/updateGateway", |
|
|
|
"/reslevis/postTracker", |
|
|
|
"/reslevis/updateTracker", |
|
|
|
"/reslevis/postTrack", |
|
|
|
"/reslevis/updateTrack", |
|
|
|
"/reslevis/postZone", |
|
|
|
"/reslevis/updateZone", |
|
|
|
} |
|
|
|
|
|
|
|
PREFIX_PROXY_PATHS = { |
|
|
|
"/reslevis/removeGateway/", |
|
|
|
"/reslevis/removeTracker/", |
|
|
|
"/reslevis/removeTrack/", |
|
|
|
"/reslevis/removeZone/", |
|
|
|
} |
|
|
|
|
|
|
|
def _should_proxy(path: str) -> bool: |
|
|
|
return path in EXACT_PROXY_PATHS or any(path.startswith(p) for p in PREFIX_PROXY_PATHS) |
|
|
|
|
|
|
|
#ResLevis CORE middleware |
|
|
|
@app.middleware("http") |
|
|
|
async def local_then_core(request: Request, call_next): |
|
|
|
# only proxy CRUD for Reslevis (change prefix or methods if needed) |
|
|
|
if request.url.path.startswith("/reslevis/") and request.method in {"POST", "PUT", "DELETE", "PATCH"}: |
|
|
|
body = await request.body() # raw body preserved |
|
|
|
local_resp = await call_next(request) # local storage runs here |
|
|
|
if request.method in {"POST", "PUT", "DELETE", "PATCH"} and _should_proxy(request.url.path): |
|
|
|
body = await request.body() |
|
|
|
local_resp = await call_next(request) |
|
|
|
if local_resp.status_code >= 400: |
|
|
|
return local_resp # stop if local failed |
|
|
|
return local_resp |
|
|
|
return await _forward_to_core(request, body) |
|
|
|
return await call_next(request) |
|
|
|
|
|
|
|
|