From e231333bcd16497575f4adaa044b040eb3742c98 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 28 May 2024 09:50:17 -0700 Subject: [PATCH] refac --- backend/main.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/backend/main.py b/backend/main.py index 3bb4f0f6b..b5a1569b8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -12,6 +12,7 @@ import mimetypes from fastapi import FastAPI, Request, Depends, status from fastapi.staticfiles import StaticFiles +from fastapi.responses import JSONResponse from fastapi import HTTPException from fastapi.middleware.wsgi import WSGIMiddleware from fastapi.middleware.cors import CORSMiddleware @@ -123,15 +124,6 @@ app.state.MODELS = {} origins = ["*"] -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - # Custom middleware to add security headers # class SecurityHeadersMiddleware(BaseHTTPMiddleware): # async def dispatch(self, request: Request, call_next): @@ -276,10 +268,8 @@ class PipelineMiddleware(BaseHTTPMiddleware): except: pass - print(sorted_filters) - for filter in sorted_filters: - + r = None try: urlIdx = filter["urlIdx"] @@ -303,7 +293,20 @@ class PipelineMiddleware(BaseHTTPMiddleware): except Exception as e: # Handle connection error here print(f"Connection error: {e}") - pass + + if r is not None: + try: + res = r.json() + if "detail" in res: + return JSONResponse( + status_code=r.status_code, + content=res, + ) + except: + pass + + else: + pass modified_body_bytes = json.dumps(data).encode("utf-8") # Replace the request body with the modified one @@ -328,6 +331,15 @@ class PipelineMiddleware(BaseHTTPMiddleware): app.add_middleware(PipelineMiddleware) +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + @app.middleware("http") async def check_url(request: Request, call_next): if len(app.state.MODELS) == 0: