From a3af1d27f2484860a3d14b53599f7fe17251f162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Thu, 29 Feb 2024 01:59:53 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20and=20update=20?= =?UTF-8?q?CORS,=20remove=20trailing=20slash=20from=20new=20Pydantic=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/.env | 2 +- src/backend/app/core/config.py | 14 ++++++++------ src/backend/app/main.py | 4 +++- src/docker-compose.yml | 1 - 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/.env b/src/.env index 2cf8e643e9..f63ba004d8 100644 --- a/src/.env +++ b/src/.env @@ -7,7 +7,7 @@ PROJECT_NAME="FastAPI Project" STACK_NAME=fastapi-project # Backend -BACKEND_CORS_ORIGINS="[\"http://localhost\", \"http://localhost:4200\", \"http://localhost:3000\", \"http://localhost:8080\", \"https://localhost\", \"https://localhost:4200\", \"https://localhost:3000\", \"https://localhost:8080\", \"http://local.dockertoolbox.tiangolo.com\", \"http://localhost.tiangolo.com\"]" +BACKEND_CORS_ORIGINS="http://localhost,http://localhost:5173,https://localhost,https://localhost:5173,http://localhost.tiangolo.com" SECRET_KEY=changethis FIRST_SUPERUSER=admin@example.com FIRST_SUPERUSER_PASSWORD=changethis diff --git a/src/backend/app/core/config.py b/src/backend/app/core/config.py index baed6888df..5341e0ad4d 100644 --- a/src/backend/app/core/config.py +++ b/src/backend/app/core/config.py @@ -16,12 +16,11 @@ class Settings(BaseSettings): SECRET_KEY: str = secrets.token_urlsafe(32) # 60 minutes * 24 hours * 8 days = 8 days ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 - SERVER_NAME: str SERVER_HOST: AnyHttpUrl # BACKEND_CORS_ORIGINS is a JSON-formatted list of origins # e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \ # "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]' - BACKEND_CORS_ORIGINS: list[AnyHttpUrl] = [] + BACKEND_CORS_ORIGINS: list[AnyHttpUrl] | str = [] @field_validator("BACKEND_CORS_ORIGINS", mode="before") @classmethod @@ -38,7 +37,7 @@ def assemble_cors_origins(cls, v: str | list[str]) -> list[str] | str: @field_validator("SENTRY_DSN", mode="before") @classmethod def sentry_dsn_can_be_blank(cls, v: str) -> str | None: - if len(v) == 0: + if not v: return None return v @@ -65,7 +64,8 @@ def assemble_db_connection(cls, v: str | None, info: ValidationInfo) -> Any: SMTP_HOST: str | None = None SMTP_USER: str | None = None SMTP_PASSWORD: str | None = None - EMAILS_FROM_EMAIL: str | None = None #TODO: update type to EmailStr when sqlmodel supports it + # TODO: update type to EmailStr when sqlmodel supports it + EMAILS_FROM_EMAIL: str | None = None EMAILS_FROM_NAME: str | None = None @field_validator("EMAILS_FROM_NAME") @@ -86,8 +86,10 @@ def get_emails_enabled(cls, v: bool, info: ValidationInfo) -> bool: and info.data.get("EMAILS_FROM_EMAIL") ) - EMAIL_TEST_USER: str = "test@example.com" #TODO: update type to EmailStr when sqlmodel supports it - FIRST_SUPERUSER: str #TODO: update type to EmailStr when sqlmodel supports it + # TODO: update type to EmailStr when sqlmodel supports it + EMAIL_TEST_USER: str = "test@example.com" + # TODO: update type to EmailStr when sqlmodel supports it + FIRST_SUPERUSER: str FIRST_SUPERUSER_PASSWORD: str USERS_OPEN_REGISTRATION: bool = False model_config = SettingsConfigDict(case_sensitive=True) diff --git a/src/backend/app/main.py b/src/backend/app/main.py index 006345dab2..9722c944f9 100644 --- a/src/backend/app/main.py +++ b/src/backend/app/main.py @@ -20,7 +20,9 @@ def custom_generate_unique_id(route: APIRoute): if settings.BACKEND_CORS_ORIGINS: app.add_middleware( CORSMiddleware, - allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS], + allow_origins=[ + str(origin).strip("/") for origin in settings.BACKEND_CORS_ORIGINS + ], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/src/docker-compose.yml b/src/docker-compose.yml index 271dbf8909..bbbe66c0b4 100644 --- a/src/docker-compose.yml +++ b/src/docker-compose.yml @@ -155,7 +155,6 @@ services: env_file: - .env environment: - - SERVER_NAME=${DOMAIN?Variable not set} - SERVER_HOST=https://${DOMAIN?Variable not set} # Allow explicit env var override for tests - SMTP_HOST=${SMTP_HOST?Variable not set}