Refactor backend auth, setup, router, and runtime state handling

This commit is contained in:
2026-04-10 21:00:36 +02:00
parent 3371ff8324
commit f61d497e4e
4 changed files with 33 additions and 24 deletions

View File

@@ -98,22 +98,23 @@ async def run_setup(
await settings_repo.set_setting(db, _KEY_MAP_COLOR_THRESHOLD_MEDIUM, "50")
await settings_repo.set_setting(db, _KEY_MAP_COLOR_THRESHOLD_LOW, "20")
await _ensure_database_initialized(database_path)
runtime_initialized = await _ensure_database_initialized(database_path)
runtime_db: aiosqlite.Connection | None = None
try:
runtime_db = await open_db(database_path)
await settings_repo.set_setting(runtime_db, _KEY_PASSWORD_HASH, hashed)
await settings_repo.set_setting(runtime_db, _KEY_DATABASE_PATH, database_path)
await settings_repo.set_setting(runtime_db, _KEY_FAIL2BAN_SOCKET, fail2ban_socket)
await settings_repo.set_setting(runtime_db, _KEY_TIMEZONE, timezone)
await settings_repo.set_setting(
runtime_db, _KEY_SESSION_DURATION, str(session_duration_minutes)
)
await settings_repo.set_setting(runtime_db, _KEY_MAP_COLOR_THRESHOLD_HIGH, "100")
await settings_repo.set_setting(runtime_db, _KEY_MAP_COLOR_THRESHOLD_MEDIUM, "50")
await settings_repo.set_setting(runtime_db, _KEY_MAP_COLOR_THRESHOLD_LOW, "20")
await settings_repo.set_setting(runtime_db, _KEY_SETUP_DONE, "1")
if runtime_initialized:
runtime_db = await open_db(database_path)
await settings_repo.set_setting(runtime_db, _KEY_PASSWORD_HASH, hashed)
await settings_repo.set_setting(runtime_db, _KEY_DATABASE_PATH, database_path)
await settings_repo.set_setting(runtime_db, _KEY_FAIL2BAN_SOCKET, fail2ban_socket)
await settings_repo.set_setting(runtime_db, _KEY_TIMEZONE, timezone)
await settings_repo.set_setting(
runtime_db, _KEY_SESSION_DURATION, str(session_duration_minutes)
)
await settings_repo.set_setting(runtime_db, _KEY_MAP_COLOR_THRESHOLD_HIGH, "100")
await settings_repo.set_setting(runtime_db, _KEY_MAP_COLOR_THRESHOLD_MEDIUM, "50")
await settings_repo.set_setting(runtime_db, _KEY_MAP_COLOR_THRESHOLD_LOW, "20")
await settings_repo.set_setting(runtime_db, _KEY_SETUP_DONE, "1")
finally:
if runtime_db is not None:
await runtime_db.close()
@@ -166,7 +167,7 @@ async def get_persisted_runtime_settings(db: aiosqlite.Connection) -> dict[str,
return runtime_settings
async def _ensure_database_initialized(database_path: str) -> None:
async def _ensure_database_initialized(database_path: str) -> bool:
"""Create and initialise the configured runtime database if it does not exist."""
database_path_obj = Path(database_path)
parent_dir = database_path_obj.parent
@@ -179,13 +180,14 @@ async def _ensure_database_initialized(database_path: str) -> None:
database_path=database_path,
parent=str(parent_dir),
)
return
return False
db = await open_db(str(database_path_obj))
try:
await init_db(db)
finally:
await db.close()
return True
async def get_timezone(db: aiosqlite.Connection) -> str: