Skip to content

Commit

Permalink
refactor(backend): simplify logic for EXTRA_CORS_ORIGINS validation
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jul 17, 2024
1 parent 2858940 commit d39bb5d
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/backend/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,33 +157,30 @@ class Settings(BaseSettings):
FMTM_DOMAIN: str
FMTM_DEV_PORT: Optional[str] = "7050"

EXTRA_CORS_ORIGINS: Optional[Union[str, list[str]]] = []
EXTRA_CORS_ORIGINS: Optional[str | list[str]] = []

@field_validator("EXTRA_CORS_ORIGINS", mode="before")
@classmethod
def assemble_cors_origins(
cls,
val: Union[str, list[str]],
val: Optional[str | list[str]],
info: ValidationInfo,
) -> Union[list[str], str]:
"""Build and validate CORS origins list.
By default, the provided frontend URLs are included in the origins list.
If this variable used, the provided urls are appended to the list.
"""
default_origins = []

# Build default origins from env vars
url_scheme = "http" if info.data.get("DEBUG") else "https"
local_server_port = (
f":{info.data.get('FMTM_DEV_PORT')}" if info.data.get("DEBUG") else ""
)
if frontend_domain := info.data.get("FMTM_DOMAIN"):
default_origins = [
f"{url_scheme}://{frontend_domain}{local_server_port}",
# Also add the xlsform-editor url
"https://xlsforms.fmtm.dev",
]
default_origins = [
f"{url_scheme}://{info.data.get('FMTM_DOMAIN')}{local_server_port}",
# Also add the xlsform-editor url
"https://xlsforms.fmtm.dev",
]

if val is None:
return default_origins
Expand Down

0 comments on commit d39bb5d

Please sign in to comment.