Use explicit AppState dependency in config router and update task status

This commit is contained in:
2026-04-07 20:15:28 +02:00
parent 59a56f2e4f
commit 30e0dd71c9
3 changed files with 22 additions and 9 deletions

View File

@@ -6,12 +6,15 @@ Routers import directly from this module — never from ``app.state``
directly — to keep coupling explicit and testable.
"""
import datetime
import time
from collections.abc import AsyncGenerator
from typing import Annotated, Protocol, cast
import aiohttp
import aiosqlite
import structlog
from apscheduler.schedulers.asyncio import AsyncIOScheduler # type: ignore[import-untyped]
from fastapi import Depends, HTTPException, Request, status
from app.config import Settings
@@ -20,9 +23,6 @@ from app.models.config import PendingRecovery
from app.models.server import ServerStatus
from app.utils.time_utils import utc_now
import aiohttp
from apscheduler.schedulers.asyncio import AsyncIOScheduler # type: ignore[import-untyped]
log: structlog.stdlib.BoundLogger = structlog.get_logger()
@@ -32,6 +32,9 @@ class AppState(Protocol):
settings: Settings
http_session: aiohttp.ClientSession
scheduler: AsyncIOScheduler
server_status: ServerStatus
pending_recovery: PendingRecovery | None
last_activation: dict[str, datetime.datetime] | None
_COOKIE_NAME = "bangui_session"
@@ -173,6 +176,11 @@ async def get_fail2ban_start_command(settings: Settings = Depends(get_settings))
"""Provide the configured fail2ban start command."""
return settings.fail2ban_start_command
async def get_app_state(request: Request) -> AppState:
"""Provide the application state object for the current request."""
return cast("AppState", request.app.state)
async def get_server_status(request: Request) -> ServerStatus:
"""Return the cached fail2ban server status snapshot from app state."""
state = cast("AppState", request.app.state)
@@ -255,4 +263,5 @@ Fail2BanConfigDirDep = Annotated[str, Depends(get_fail2ban_config_dir)]
Fail2BanStartCommandDep = Annotated[str, Depends(get_fail2ban_start_command)]
ServerStatusDep = Annotated[ServerStatus, Depends(get_server_status)]
PendingRecoveryDep = Annotated[PendingRecovery | None, Depends(get_pending_recovery)]
AppStateDep = Annotated[AppState, Depends(get_app_state)]
AuthDep = Annotated[Session, Depends(require_auth)]