]> git.giorgioravera.it Git - network-manager.git/commitdiff
Renamed db/config.py module to db/settings.py
authorGiorgio Ravera <giorgio.ravera@gmail.com>
Sun, 31 May 2026 12:27:14 +0000 (14:27 +0200)
committerGiorgio Ravera <giorgio.ravera@gmail.com>
Sun, 31 May 2026 12:27:14 +0000 (14:27 +0200)
backend/bootstrap.py
backend/db/config.py [deleted file]
backend/db/settings.py [new file with mode: 0644]
backend/routes/about.py
backend/routes/devices.py
backend/routes/dns.py
backend/routes/login.py
backend/server.py

index 594c68ec43beec6dc33888586b88fbdfe1a3c4b5..288852055955c4e818699fe78df5c4c1ea978c1d 100644 (file)
@@ -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 (file)
index 4507c74..0000000
+++ /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 (file)
index 0000000..e850011
--- /dev/null
@@ -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)))
index 058fbc17e6b13bcd4743f211bccc6e346812d948..10031f85908ec871ba874b544de6c9f7fcf85e03 100644 (file)
@@ -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()
index 684afb7d270a2a16c43f2435d60278e0001e2471..1fc42d05ef1746117066ca05fa948185ad297cd5 100644 (file)
@@ -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
 
index 55bb5d8a55e73bf9fd0ffc4e39f0000697e927ad..2e697fb681254e6c3c4c82dd5064492181e9bea3 100644 (file)
@@ -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
 
index a15cf5bb3b527edd1f3bf9f9d9c3c685a55f32a0..8152ea03870d85043b38e08d7bdeb35f6ab147de 100644 (file)
@@ -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()
index cbd7048b2cf5279885b591b78bece8aa521e1893..5872fe4f9f3af9bd91b114cd3eb61d48bcf45d19 100644 (file)
@@ -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