Enforce repository boundary: Remove DbDep from routers

This commit enforces the repository boundary by eliminating direct database connection
dependencies (DbDep) from all routers. Routers now depend on service context dependencies
that combine the database connection with the related repositories.

Changes:
- Add 5 service context dependencies in dependencies.py:
  * SessionServiceContext: db + session_repo
  * BlocklistServiceContext: db + blocklist_repo + import_log_repo + settings_repo
  * SettingsServiceContext: db + settings_repo
  * BanServiceContext: db + fail2ban_db_repo
  * HistoryServiceContext: db + fail2ban_db_repo + history_archive_repo

- Refactor all 9 routers (auth, bans, blocklist, config_misc, dashboard, geo,
  history, jails, setup) to use service contexts instead of DbDep.

- Update Backend-Development.md with clear examples of the new pattern and
  documentation of available service contexts.

Rationale:
- Enforces the repository boundary through the dependency system
- Makes database operations explicit and auditable
- Improves testability by allowing service contexts to be mocked
- Prevents accidental direct database access from routers

The deprecated DbDep remains available for backward compatibility with
services that have not yet been refactored, but routers can no longer import it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-04-28 07:35:23 +02:00
parent 813cf09bed
commit 507f153ab9
11 changed files with 318 additions and 105 deletions

View File

@@ -10,7 +10,7 @@ from __future__ import annotations
import structlog
from fastapi import APIRouter, HTTPException, status
from app.dependencies import AppDep, DbDep, SettingsDep
from app.dependencies import AppDep, SettingsDep, SettingsServiceContextDep
from app.models.setup import SetupRequest, SetupResponse, SetupStatusResponse, SetupTimezoneResponse
from app.services import setup_service
from app.utils.runtime_state import update_app_settings
@@ -46,14 +46,14 @@ async def get_setup_status(app: AppDep) -> SetupStatusResponse:
async def post_setup(
app: AppDep,
body: SetupRequest,
db: DbDep,
settings_ctx: SettingsServiceContextDep,
) -> SetupResponse:
"""Persist the initial BanGUI configuration.
Args:
app: The FastAPI application instance.
body: Setup request payload validated by Pydantic.
db: Injected aiosqlite connection.
settings_ctx: Settings service context containing db and repository.
Returns:
:class:`~app.models.setup.SetupResponse` on success.
@@ -61,14 +61,14 @@ async def post_setup(
Raises:
HTTPException: 409 if setup has already been completed.
"""
if is_setup_complete_cached(app) or await setup_service.is_setup_complete(db):
if is_setup_complete_cached(app) or await setup_service.is_setup_complete(settings_ctx.db):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Setup has already been completed.",
)
await setup_service.run_setup(
db,
settings_ctx.db,
master_password=body.master_password,
database_path=body.database_path,
fail2ban_socket=body.fail2ban_socket,