diff options
Diffstat (limited to 'thallium-backend/src/settings.py')
| -rw-r--r-- | thallium-backend/src/settings.py | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/thallium-backend/src/settings.py b/thallium-backend/src/settings.py index 2f9f6c1..931fc0d 100644 --- a/thallium-backend/src/settings.py +++ b/thallium-backend/src/settings.py @@ -1,9 +1,46 @@ -from pydantic_settings import BaseSettings +import typing +from collections.abc import AsyncGenerator +from logging import getLogger +import pydantic +import pydantic_settings +from fastapi import Depends +from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine + +log = getLogger(__name__) + + +class _Config( + pydantic_settings.BaseSettings, + env_prefix="backend_", + env_file=".env", + env_file_encoding="utf-8", + env_nested_delimiter="__", + extra="ignore", +): + """General configuration settings for the service.""" -class _CONFIG(BaseSettings, env_file=".env", env_file_encoding="utf-8"): debug: bool = False git_sha: str = "development" + database_url: pydantic.SecretStr + token: pydantic.SecretStr + + +CONFIG = _Config() + + +class Connections: + """How to connect to other, internal services.""" + + DB_ENGINE = create_async_engine(CONFIG.database_url.get_secret_value(), echo=CONFIG.debug) + DB_SESSION_MAKER = async_sessionmaker(DB_ENGINE) + + +async def _get_db_session() -> AsyncGenerator[AsyncSession, None]: + """Yield a database session, for use with a FastAPI dependency.""" + async with Connections.DB_SESSION_MAKER() as session, session.begin(): + yield session + -CONFIG = _CONFIG() +DBSession = typing.Annotated[AsyncSession, Depends(_get_db_session)] |