www_site/services/api/app/config.py

47 lines
1.7 KiB
Python
Raw Normal View History

import os
from dataclasses import dataclass
def as_bool(value: str | None, default: bool = False) -> bool:
if value is None:
return default
return value.lower() in {"1", "true", "yes", "on"}
@dataclass(frozen=True)
class Settings:
database_url: str
app_env: str
allowed_origins: tuple[str, ...]
session_cookie_name: str
session_cookie_secure: bool
session_ttl_seconds: int
csrf_secret: str
email_verification_enabled: bool
phone_verification_enabled: bool
test_free_period_limit: int
test_vip_period_limit: int
@classmethod
def from_env(cls) -> "Settings":
database_url = os.getenv("DATABASE_URL")
if not database_url:
raise RuntimeError("DATABASE_URL is required")
origins = tuple(item.strip().rstrip("/") for item in os.getenv("ALLOWED_ORIGINS", "").split(",") if item.strip())
return cls(
database_url=database_url,
app_env=os.getenv("APP_ENV", "test"),
allowed_origins=origins,
session_cookie_name=os.getenv("SESSION_COOKIE_NAME", "kaotings_session"),
session_cookie_secure=as_bool(os.getenv("SESSION_COOKIE_SECURE")),
session_ttl_seconds=int(os.getenv("SESSION_TTL_SECONDS", "2592000")),
csrf_secret=os.getenv("CSRF_SECRET", "development-only-change-me"),
email_verification_enabled=as_bool(os.getenv("EMAIL_VERIFICATION_ENABLED")),
phone_verification_enabled=as_bool(os.getenv("PHONE_VERIFICATION_ENABLED")),
test_free_period_limit=int(os.getenv("TEST_FREE_PERIOD_LIMIT", "10000")),
test_vip_period_limit=int(os.getenv("TEST_VIP_PERIOD_LIMIT", "100000")),
)
settings = Settings.from_env()