From 72cb9374adb92a9901ba2486d3d0c4aaf95b3d42 Mon Sep 17 00:00:00 2001 From: Emillio Mariscal Date: Thu, 14 Dec 2023 16:44:03 -0300 Subject: [PATCH 1/2] + Customizable CORS for API --- python/restapi/config.py | 9 ++++++++- python/restapi/main.py | 10 +++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/python/restapi/config.py b/python/restapi/config.py index e71dbf5d..364ad0e1 100644 --- a/python/restapi/config.py +++ b/python/restapi/config.py @@ -1,4 +1,11 @@ import os # ENABLE_UNDERPASS_CORE=True -UNDERPASS_DB=os.getenv("UNDERPASS_API_DB") or "postgresql://localhost/underpass" \ No newline at end of file +UNDERPASS_DB=os.getenv("UNDERPASS_API_DB") or "postgresql://localhost/underpass" +ORIGINS = os.getenv("UNDERPASS_API_ORIGINS").split(",") if os.getenv("UNDERPASS_API_ORIGINS") else [ + "http://localhost", + "http://localhost:5000", + "http://localhost:3000", + "http://127.0.0.1", + "http://127.0.0.1:5000" +] \ No newline at end of file diff --git a/python/restapi/main.py b/python/restapi/main.py index c5751b2d..840d0b7f 100644 --- a/python/restapi/main.py +++ b/python/restapi/main.py @@ -48,13 +48,9 @@ import config import json -origins = [ - "http://localhost", - "http://localhost:5000", - "http://localhost:3000", - "http://127.0.0.1", - "http://127.0.0.1:5000" -] +origins = config.ORIGINS + +print(origins) app = FastAPI() From 90c5935c4b7bf92387c053173f275f2b8c89aedc Mon Sep 17 00:00:00 2001 From: Emillio Mariscal Date: Thu, 14 Dec 2023 16:46:33 -0300 Subject: [PATCH 2/2] Remove debug message, improve code --- python/restapi/main.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/python/restapi/main.py b/python/restapi/main.py index 840d0b7f..0b5033bd 100644 --- a/python/restapi/main.py +++ b/python/restapi/main.py @@ -48,15 +48,11 @@ import config import json -origins = config.ORIGINS - -print(origins) - app = FastAPI() app.add_middleware( CORSMiddleware, - allow_origins=origins, + allow_origins=config.ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"]