Refactor app helpers and use AppStateDep in config router

Move service-dependent helper wrappers from app.utils to app.helpers and update config router activation/rollback to use explicit AppState dependency.
This commit is contained in:
2026-04-07 20:39:56 +02:00
parent ed3aa61c35
commit 1e39e5a1d6
11 changed files with 98 additions and 30 deletions

View File

@@ -678,6 +678,7 @@ async def update_map_color_thresholds(
summary="Activate an inactive jail",
)
async def activate_jail(
app_state: AppStateDep,
app: AppDep,
_auth: AuthDep,
config_dir: Fail2BanConfigDirDep,
@@ -692,6 +693,7 @@ async def activate_jail(
the jail starts immediately.
Args:
app_state: FastAPI application state.
app: FastAPI application instance.
_auth: Validated session.
name: Name of the jail to activate.
@@ -730,7 +732,7 @@ async def activate_jail(
# Record this activation so the health-check task can attribute a
# subsequent fail2ban crash to it.
app.state.last_activation = {
app_state.last_activation = {
"jail_name": name,
"at": datetime.datetime.now(tz=datetime.UTC),
}
@@ -738,9 +740,9 @@ async def activate_jail(
# If fail2ban stopped responding after the reload, create a pending-recovery
# record immediately (before the background health task notices).
if not result.fail2ban_running:
app.state.pending_recovery = PendingRecovery(
app_state.pending_recovery = PendingRecovery(
jail_name=name,
activated_at=app.state.last_activation["at"],
activated_at=app_state.last_activation["at"],
detected_at=datetime.datetime.now(tz=datetime.UTC),
)
@@ -932,7 +934,6 @@ async def get_pending_recovery(
summary="Disable a bad jail config and restart fail2ban",
)
async def rollback_jail(
request: Request,
_auth: AuthDep,
app_state: AppStateDep,
config_dir: Fail2BanConfigDirDep,
@@ -949,7 +950,6 @@ async def rollback_jail(
On success, clears the :class:`~app.models.config.PendingRecovery` record.
Args:
request: FastAPI request object.
_auth: Validated session.
name: Jail name to disable and roll back.