|
| 1 | +"""App configuration.""" |
| 2 | +import os |
| 3 | + |
| 4 | +from pydantic import Field |
| 5 | +from pydantic_settings import BaseSettings |
| 6 | +from pydantic_settings import SettingsConfigDict |
| 7 | + |
| 8 | + |
| 9 | +class Settings(BaseSettings): |
| 10 | + secret_key: bytes = os.urandom(24) |
| 11 | + sqlalchemy_database_uri: str = Field( |
| 12 | + alias="DATABASE_URL", |
| 13 | + default="postgresql+asyncpg://localhost:5432/consent_api", |
| 14 | + ) |
| 15 | + consent_expiry_days: int = 7 |
| 16 | + consent_api_origin: str | None = None |
| 17 | + other_service_origin: str | None = None |
| 18 | + flag_fixtures: bool = True |
| 19 | + |
| 20 | + model_config = SettingsConfigDict(env_file=".env", extra="ignore") |
| 21 | + |
| 22 | + |
| 23 | +class Production(Settings): |
| 24 | + ... |
| 25 | + |
| 26 | + |
| 27 | +class Staging(Settings): |
| 28 | + consent_api_origin: str = "https://gds-single-consent-staging.app" |
| 29 | + |
| 30 | + |
| 31 | +class Testing(Settings): |
| 32 | + consent_api_origin: str = "http://consent-api" |
| 33 | + other_service_origin: str = "http://dummy-service-1" |
| 34 | + |
| 35 | + |
| 36 | +class Development(Settings): |
| 37 | + ... |
| 38 | + |
| 39 | + |
| 40 | +def get_settings(): |
| 41 | + env = os.getenv("ENV", "development") |
| 42 | + try: |
| 43 | + return {subclass.__name__: subclass for subclass in Settings.__subclasses__()}[ |
| 44 | + env.title() |
| 45 | + ]() |
| 46 | + except KeyError as err: |
| 47 | + raise ValueError(f"Invalid ENV={env}") from err |
| 48 | + |
| 49 | + |
| 50 | +settings = get_settings() |
| 51 | + |
| 52 | +print(f"Environment: {settings.__class__.__name__}") |
0 commit comments