Files
test-litestar-addressbook/app/lib/settings.py
Eden Kirin f57c4d4491 Tweaks
2023-09-15 19:43:58 +02:00

127 lines
3.1 KiB
Python

"""All configuration via environment.
Take note of the environment variable prefixes required for each
settings class, except `AppSettings`.
"""
from typing import Literal, Optional, Union
__all__ = [
"APISettings",
"AppSettings",
"DatabaseSettings",
"EmailSettings",
"OpenAPISettings",
"ServerSettings",
]
from pydantic import Extra
from pydantic_settings import BaseSettings
class BaseEnvSettings(BaseSettings):
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
extra = Extra.ignore
class AppSettings(BaseEnvSettings):
class Config:
case_sensitive = True
BUILD_NUMBER: str = "0"
DEBUG: bool = False
ENVIRONMENT: str = "local"
LOG_LEVEL: str = "INFO"
NAME: str = "addressbook"
@property
def slug(self) -> str:
return "-".join(s.lower() for s in self.NAME.split())
class APISettings(BaseEnvSettings):
class Config:
env_prefix = "API_"
case_sensitive = True
CACHE_EXPIRATION: int = 60
DB_SESSION_DEPENDENCY_KEY: str = "db_session"
DEFAULT_PAGINATION_LIMIT: int = 100
DEFAULT_USER_NAME: str = "__default_user__"
HEALTH_PATH: str = "/health"
SECRET_KEY: str = "abc123"
USER_DEPENDENCY_KEY: str = "user"
class OpenAPISettings(BaseEnvSettings):
class Config:
env_prefix = "OPENAPI_"
case_sensitive = True
TITLE: Optional[str] = "My Litestar App"
VERSION: str = "0.1.0"
CONTACT_NAME: str = "My Name"
CONTACT_EMAIL: str = "some_human@some_domain.com"
class DatabaseSettings(BaseEnvSettings):
class Config:
env_prefix = "DB_"
case_sensitive = True
ECHO: bool = False
ECHO_POOL: Union[bool, Literal["debug"]] = False
POOL_DISABLE: bool = False
POOL_MAX_OVERFLOW: int = 10
POOL_SIZE: int = 5
POOL_TIMEOUT: int = 30
HOST: str = "localhost"
PORT: int = 5432
NAME: str = "db-name"
USER: str = "db-user"
PASSWORD: str = "db-password"
FLYWAY_PATH: str = "/usr/local/bin/flyway"
class TestingSettings(BaseEnvSettings):
class Config:
env_prefix = "TESTS_"
case_sensitive = True
DB_HOST: str = "localhost"
DB_PORT: int = 5432
DB_NAME: str = "test_db-name"
DB_USER: str = "db-user"
DB_PASSWORD: str = "db-password"
DROP_DATABASE_BEFORE_TESTS: bool = True
DROP_DATABASE_AFTER_TESTS: bool = True
class ServerSettings(BaseEnvSettings):
class Config:
env_prefix = "UVICORN_"
case_sensitive = True
HOST: str = "localhost"
LOG_LEVEL: str = "info"
PORT: int = 8000
RELOAD: bool = True
KEEPALIVE: int = 65
class EmailSettings(BaseEnvSettings):
class Config:
env_prefix = "EMAIL_"
case_sensitive = True
# `.parse_obj()` thing is a workaround for pyright and pydantic interplay, see:
# https://github.com/pydantic/pydantic/issues/3753#issuecomment-1087417884
api = APISettings.parse_obj({})
app = AppSettings.parse_obj({})
db = DatabaseSettings.parse_obj({})
openapi = OpenAPISettings.parse_obj({})
server = ServerSettings.parse_obj({})
testing = TestingSettings.parse_obj({})