Files
template-fastapi/{{project_slug}}/dependencies/connection_manager_dep.py.jinja
Aleksei Sokol 6e9ed87ec4
Some checks failed
Run linters on applied template / Python 3.13 lint and build (push) Failing after 39s
Initial commit
This is a FastAPI backend microservice template used with `copier` utility.

Features of applied template are:
- Configuration file processing logic
- Metrics and tracing (both optional) configuration available
- Debug endpoints
- Database migration commands, prepared Alembic environment
- Database usage example in ping_db endpoint
- gitea sanity check pipeline
2025-11-29 22:01:15 +03:00

26 lines
1.1 KiB
Django/Jinja

"""PostgresConnectionManager dependency functions are defined here."""
from fastapi import FastAPI, Request
from {{project_slug}}.db.connection.manager import PostgresConnectionManager
def init_dispencer(app: FastAPI, connection_manager: PostgresConnectionManager) -> None:
"""Initialize PostgresConnectionManager dispencer at app's state."""
if hasattr(app.state, "postgres_connection_manager"):
if not isinstance(app.state.postgres_connection_manager, PostgresConnectionManager):
raise ValueError(
"postgres_connection_manager attribute of app's state is already set"
f"with other value ({app.state.postgres_connection_manager})"
)
return
app.state.postgres_connection_manager = connection_manager
def obtain(request: Request) -> PostgresConnectionManager:
"""Get a PostgresConnectionManager from request's app state."""
if not hasattr(request.app.state, "postgres_connection_manager"):
raise ValueError("PostgresConnectionManager dispencer was not initialized at app preparation")
return request.app.state.postgres_connection_manager