formaptix-server/models/settings.py

41 lines
1 KiB
Python
Raw Normal View History

2024-09-22 11:34:48 +03:00
from typing import Annotated, Any
from pydantic import AnyUrl, BeforeValidator, computed_field
from pydantic_settings import BaseSettings, SettingsConfigDict
def parse_cors(v: Any) -> list[str] | str:
if isinstance(v, str) and not v.startswith("["):
return [i.strip() for i in v.split(",")]
elif isinstance(v, list | str):
return v
raise ValueError(v)
2024-08-08 22:14:02 +03:00
class Settings(BaseSettings):
2024-09-22 11:34:48 +03:00
model_config = SettingsConfigDict(
env_file=".env", env_ignore_empty=True, extra="ignore"
)
DATABASE: str
SECRET: str
PORT: int
FRONTEND_HOST: str = "http://localhost:5173"
BACKEND_CORS_ORIGINS: Annotated[list[AnyUrl] | str, BeforeValidator(parse_cors)] = (
[]
)
@computed_field # type: ignore[prop-decorator]
@property
def all_cors_origins(self) -> list[str]:
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
self.FRONTEND_HOST
]
ADMIN_PASSWORD: str
DISABLE_ADMIN: bool = False
2024-08-08 22:14:02 +03:00
settings = Settings()