Fix setup persistence and load persisted runtime configuration
This commit is contained in:
@@ -8,6 +8,7 @@ enforcing the rule that setup can only run once.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import bcrypt
|
||||
@@ -16,6 +17,7 @@ import structlog
|
||||
if TYPE_CHECKING:
|
||||
import aiosqlite
|
||||
|
||||
from app.db import init_db, open_db
|
||||
from app.repositories import settings_repo
|
||||
|
||||
log: structlog.stdlib.BoundLogger = structlog.get_logger()
|
||||
@@ -95,6 +97,9 @@ async def run_setup(
|
||||
await settings_repo.set_setting(db, _KEY_MAP_COLOR_THRESHOLD_HIGH, "100")
|
||||
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)
|
||||
|
||||
# Mark setup as complete — must be last so a partial failure leaves
|
||||
# setup_completed unset and does not lock out the user.
|
||||
await settings_repo.set_setting(db, _KEY_SETUP_DONE, "1")
|
||||
@@ -114,6 +119,58 @@ async def get_password_hash(db: aiosqlite.Connection) -> str | None:
|
||||
return await util_get_password_hash(db)
|
||||
|
||||
|
||||
async def get_persisted_runtime_settings(db: aiosqlite.Connection) -> dict[str, str | int]:
|
||||
"""Return runtime configuration values persisted during initial setup."""
|
||||
runtime_settings: dict[str, str | int] = {}
|
||||
|
||||
database_path = await settings_repo.get_setting(db, _KEY_DATABASE_PATH)
|
||||
if database_path:
|
||||
runtime_settings["database_path"] = database_path
|
||||
|
||||
fail2ban_socket = await settings_repo.get_setting(db, _KEY_FAIL2BAN_SOCKET)
|
||||
if fail2ban_socket:
|
||||
runtime_settings["fail2ban_socket"] = fail2ban_socket
|
||||
|
||||
timezone = await settings_repo.get_setting(db, _KEY_TIMEZONE)
|
||||
if timezone:
|
||||
runtime_settings["timezone"] = timezone
|
||||
|
||||
session_duration = await settings_repo.get_setting(db, _KEY_SESSION_DURATION)
|
||||
if session_duration is not None:
|
||||
try:
|
||||
runtime_settings["session_duration_minutes"] = int(session_duration)
|
||||
except ValueError:
|
||||
log.warning(
|
||||
"invalid_setup_setting",
|
||||
key=_KEY_SESSION_DURATION,
|
||||
value=session_duration,
|
||||
)
|
||||
|
||||
return runtime_settings
|
||||
|
||||
|
||||
async def _ensure_database_initialized(database_path: str) -> None:
|
||||
"""Create and initialise the configured runtime database if it does not exist."""
|
||||
database_path_obj = Path(database_path)
|
||||
parent_dir = database_path_obj.parent
|
||||
|
||||
try:
|
||||
parent_dir.mkdir(parents=True, exist_ok=True)
|
||||
except PermissionError:
|
||||
log.warning(
|
||||
"cannot_create_runtime_database_parent",
|
||||
database_path=database_path,
|
||||
parent=str(parent_dir),
|
||||
)
|
||||
return
|
||||
|
||||
db = await open_db(str(database_path_obj))
|
||||
try:
|
||||
await init_db(db)
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
async def get_timezone(db: aiosqlite.Connection) -> str:
|
||||
"""Return the configured IANA timezone string."""
|
||||
tz = await settings_repo.get_setting(db, _KEY_TIMEZONE)
|
||||
|
||||
Reference in New Issue
Block a user