From: Giorgio Ravera Date: Sun, 31 May 2026 12:27:14 +0000 (+0200) Subject: Renamed db/config.py module to db/settings.py X-Git-Url: http://git.giorgioravera.it/?a=commitdiff_plain;h=ead2f84af0568d32bb781a1eaa385592bc81b92e;p=network-manager.git Renamed db/config.py module to db/settings.py --- diff --git a/backend/bootstrap.py b/backend/bootstrap.py index 594c68e..2888520 100644 --- a/backend/bootstrap.py +++ b/backend/bootstrap.py @@ -5,14 +5,14 @@ import logging # Import backend modules from backend.db.db import configure_db, create_db -import backend.db.config +import backend.db.settings import backend.db.users import backend.db.hosts import backend.db.aliases # Import Settings & Config from backend.settings.settings import settings -from backend.db.config import get_config +from backend.db.settings import get_config # Import Logging from backend.log.log import setup_logging, get_logger diff --git a/backend/db/config.py b/backend/db/config.py deleted file mode 100644 index 4507c74..0000000 --- a/backend/db/config.py +++ /dev/null @@ -1,146 +0,0 @@ -# backend/db/config.py - -# Import local modules -from backend.db.db import get_db, register_init -from backend.utils import to_bool - -# Import Settings -from backend.settings.settings import settings -# Import Logging -from backend.log.log import get_logger - -# Logger initialization -logger = get_logger(__name__) - -# --------------------------------------------------------- -# Internal: wrapper to to_bool -# --------------------------------------------------------- -def _to_bool(v): - result = to_bool(v) - return result if result is not None else False - -# --------------------------------------------------------- -# Type mapping for config keys -# --------------------------------------------------------- -CONFIG_TYPES = { - "LOG_LEVEL": str, - "LOG_TO_FILE": _to_bool, - "EXTERNAL_NAME": str, - "LOGIN_MAX_ATTEMPTS": int, - "LOGIN_WINDOW_SECONDS": int, - "PING_WORKERS": int, -} - -# --------------------------------------------------------- -# Default Values -# --------------------------------------------------------- -CONFIG_DEFAULTS = { - "LOG_LEVEL": settings.LOG_LEVEL, - "LOG_TO_FILE": settings.LOG_TO_FILE, - "EXTERNAL_NAME": settings.EXTERNAL_NAME, - "LOGIN_MAX_ATTEMPTS": settings.LOGIN_MAX_ATTEMPTS, - "LOGIN_WINDOW_SECONDS": settings.LOGIN_WINDOW_SECONDS, - "PING_WORKERS": settings.PING_WORKERS, -} - -# --------------------------------------------------------- -# Runtime cache to avoid repeated DB queries -# --------------------------------------------------------- -_config_cache = {} - -# --------------------------------------------------------- -# Clear cache -# --------------------------------------------------------- -def clear_cache(key=None): - """Clear cached config entry (or full cache).""" - if key: - _config_cache.pop(key, None) - else: - _config_cache.clear() - -# --------------------------------------------------------- -# Return a specific config value (with cache + type casting) -# --------------------------------------------------------- -def set_config(key, value): - if key not in CONFIG_TYPES: - logger.warning("Config key not typed: %s", key) - - # salva sempre come stringa - str_value = str(value) - - conn = get_db() - conn.execute( - "INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", - (key, str_value), - ) - conn.commit() - - # invalida cache - clear_cache(key) - -# --------------------------------------------------------- -# Return a specific config value (with cache + type casting) -# --------------------------------------------------------- -def get_config(key): - - if key not in CONFIG_TYPES: - logger.warning("Invalid config key: %s", key) - - # ---- Cache hit ---- - if key in _config_cache: - return _config_cache[key] - - # ---- Read from DB ---- - conn = get_db() - cur = conn.execute("SELECT value FROM config WHERE key = ?", (key,)) - row = cur.fetchone() - - if not row: - value = getattr(settings, key, None) - logger.warning("Config key not found in database: %s (using default: %s)", key, value) - _config_cache[key] = value - return value - - raw_value = row["value"] - - # ---- Type casting ---- - caster = CONFIG_TYPES.get(key, str) - try: - value = caster(raw_value) - except Exception: - value = raw_value # fallback safe - - # ---- Save in cache ---- - _config_cache[key] = value - return value - -# --------------------------------------------------------- -# Return a specific config value or default -# --------------------------------------------------------- -def get_config_or(key, default): - value = get_config(key) - return value if value is not None else default - -# --------------------------------------------------------- -# Create Config DB Tables -# --------------------------------------------------------- -@register_init -def init_db_config_table(cur): - - # CONFIG TABLE - cur.execute(""" - CREATE TABLE IF NOT EXISTS config ( - key TEXT PRIMARY KEY, - value TEXT - ); - """) - -# --------------------------------------------------------- -# Initialize Config DB Tables -# --------------------------------------------------------- -@register_init -def init_db_config_defaults(cur): - - # Add configuration parameters - for key, value in CONFIG_DEFAULTS.items(): - cur.execute("INSERT OR IGNORE INTO config VALUES (?, ?)", (key, str(value))) diff --git a/backend/db/settings.py b/backend/db/settings.py new file mode 100644 index 0000000..e850011 --- /dev/null +++ b/backend/db/settings.py @@ -0,0 +1,146 @@ +# backend/db/settings.py + +# Import local modules +from backend.db.db import get_db, register_init +from backend.utils import to_bool + +# Import Settings +from backend.settings.settings import settings +# Import Logging +from backend.log.log import get_logger + +# Logger initialization +logger = get_logger(__name__) + +# --------------------------------------------------------- +# Internal: wrapper to to_bool +# --------------------------------------------------------- +def _to_bool(v): + result = to_bool(v) + return result if result is not None else False + +# --------------------------------------------------------- +# Type mapping for config keys +# --------------------------------------------------------- +CONFIG_TYPES = { + "LOG_LEVEL": str, + "LOG_TO_FILE": _to_bool, + "EXTERNAL_NAME": str, + "LOGIN_MAX_ATTEMPTS": int, + "LOGIN_WINDOW_SECONDS": int, + "PING_WORKERS": int, +} + +# --------------------------------------------------------- +# Default Values +# --------------------------------------------------------- +CONFIG_DEFAULTS = { + "LOG_LEVEL": settings.LOG_LEVEL, + "LOG_TO_FILE": settings.LOG_TO_FILE, + "EXTERNAL_NAME": settings.EXTERNAL_NAME, + "LOGIN_MAX_ATTEMPTS": settings.LOGIN_MAX_ATTEMPTS, + "LOGIN_WINDOW_SECONDS": settings.LOGIN_WINDOW_SECONDS, + "PING_WORKERS": settings.PING_WORKERS, +} + +# --------------------------------------------------------- +# Runtime cache to avoid repeated DB queries +# --------------------------------------------------------- +_config_cache = {} + +# --------------------------------------------------------- +# Clear cache +# --------------------------------------------------------- +def clear_cache(key=None): + """Clear cached config entry (or full cache).""" + if key: + _config_cache.pop(key, None) + else: + _config_cache.clear() + +# --------------------------------------------------------- +# Return a specific config value (with cache + type casting) +# --------------------------------------------------------- +def set_config(key, value): + if key not in CONFIG_TYPES: + logger.warning("Config key not typed: %s", key) + + # salva sempre come stringa + str_value = str(value) + + conn = get_db() + conn.execute( + "INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", + (key, str_value), + ) + conn.commit() + + # invalida cache + clear_cache(key) + +# --------------------------------------------------------- +# Return a specific config value (with cache + type casting) +# --------------------------------------------------------- +def get_config(key): + + if key not in CONFIG_TYPES: + logger.warning("Invalid config key: %s", key) + + # ---- Cache hit ---- + if key in _config_cache: + return _config_cache[key] + + # ---- Read from DB ---- + conn = get_db() + cur = conn.execute("SELECT value FROM config WHERE key = ?", (key,)) + row = cur.fetchone() + + if not row: + value = getattr(settings, key, None) + logger.warning("Config key not found in database: %s (using default: %s)", key, value) + _config_cache[key] = value + return value + + raw_value = row["value"] + + # ---- Type casting ---- + caster = CONFIG_TYPES.get(key, str) + try: + value = caster(raw_value) + except Exception: + value = raw_value # fallback safe + + # ---- Save in cache ---- + _config_cache[key] = value + return value + +# --------------------------------------------------------- +# Return a specific config value or default +# --------------------------------------------------------- +def get_config_or(key, default): + value = get_config(key) + return value if value is not None else default + +# --------------------------------------------------------- +# Create Config DB Tables +# --------------------------------------------------------- +@register_init +def init_db_config_table(cur): + + # CONFIG TABLE + cur.execute(""" + CREATE TABLE IF NOT EXISTS config ( + key TEXT PRIMARY KEY, + value TEXT + ); + """) + +# --------------------------------------------------------- +# Initialize Config DB Tables +# --------------------------------------------------------- +@register_init +def init_db_config_defaults(cur): + + # Add configuration parameters + for key, value in CONFIG_DEFAULTS.items(): + cur.execute("INSERT OR IGNORE INTO config VALUES (?, ?)", (key, str(value))) diff --git a/backend/routes/about.py b/backend/routes/about.py index 058fbc1..10031f8 100644 --- a/backend/routes/about.py +++ b/backend/routes/about.py @@ -6,7 +6,7 @@ from datetime import datetime, timezone # Import Settings & Config from backend.settings.settings import settings -from backend.db.config import get_config +from backend.db.settings import get_config # Create Router router = APIRouter() diff --git a/backend/routes/devices.py b/backend/routes/devices.py index 684afb7..1fc42d0 100644 --- a/backend/routes/devices.py +++ b/backend/routes/devices.py @@ -13,7 +13,7 @@ from backend.db.leases import get_leases # Import Settings & Config from backend.settings.settings import settings -from backend.db.config import get_config +from backend.db.settings import get_config # Import Logging from backend.log.log import get_logger diff --git a/backend/routes/dns.py b/backend/routes/dns.py index 55bb5d8..2e697fb 100644 --- a/backend/routes/dns.py +++ b/backend/routes/dns.py @@ -14,7 +14,7 @@ from backend.db.aliases import get_aliases # Import Settings & Config from backend.settings.settings import settings -from backend.db.config import get_config +from backend.db.settings import get_config # Import Logging from backend.log.log import get_logger diff --git a/backend/routes/login.py b/backend/routes/login.py index a15cf5b..8152ea0 100644 --- a/backend/routes/login.py +++ b/backend/routes/login.py @@ -10,7 +10,7 @@ from backend.security import verify_login, apply_session, close_session # Import Settings & Config from backend.settings.settings import settings -from backend.db.config import get_config +from backend.db.settings import get_config # Create Router router = APIRouter() diff --git a/backend/server.py b/backend/server.py index cbd7048..5872fe4 100644 --- a/backend/server.py +++ b/backend/server.py @@ -5,7 +5,7 @@ import uvicorn # Import Settings & Config from backend.settings.settings import settings -from backend.db.config import get_config +from backend.db.settings import get_config # Import Logging from backend.log.log import get_logger