Files
yggdrasil/server/config.py
T

72 lines
2.1 KiB
Python
Raw Normal View History

2026-11-01 12:07:04 +01:00
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
SECRET_FILE = BASE_DIR / ".secret_key"
DEFAULT_SENSORS_PORT = 8001
2026-11-01 20:47:27 +01:00
DEFAULT_CLIPBOARD_MAX_FILE_BYTES = 100 * 1024 * 1024
DEFAULT_CLIPBOARD_MAX_TOTAL_BYTES = 1024 * 1024 * 1024
2026-11-01 12:07:04 +01:00
def load_secret_key() -> str:
"""Return a stable key used to sign the main app session cookies."""
key = os.environ.get("SECRET_KEY")
if key:
return key
if SECRET_FILE.exists():
return SECRET_FILE.read_text().strip()
return "dev-secret-key-change-me"
def sensors_database_uri() -> str:
"""Database owned by the sensors API (sensors and readouts only)."""
configured = os.environ.get("SENSORS_DATABASE_URL") or os.environ.get("DATABASE_URL")
if configured:
return configured
db_path = os.environ.get(
"SENSORS_DB_PATH", os.environ.get("DB_PATH", str(BASE_DIR / "sensors.db"))
)
return f"sqlite:///{db_path}"
def users_database_uri() -> str:
"""Database owned by the main app (user accounts only)."""
configured = os.environ.get("USERS_DATABASE_URL")
if configured:
return configured
db_path = os.environ.get("USERS_DB_PATH", str(BASE_DIR / "main_app.db"))
return f"sqlite:///{db_path}"
def sensors_api_url() -> str:
"""Base URL used by the main app to call the sensors API (server-to-server)."""
configured = os.environ.get("SENSORS_API_URL")
if configured:
return configured.rstrip("/")
return f"http://localhost:{DEFAULT_SENSORS_PORT}"
2026-11-01 20:47:27 +01:00
def clipboard_dir() -> Path:
"""Directory where uploaded clipboard files are stored."""
configured = os.environ.get("CLIPBOARD_DIR")
if configured:
return Path(configured).expanduser().resolve()
return BASE_DIR / "clipboard"
def clipboard_max_file_bytes() -> int:
return int(
os.environ.get(
"CLIPBOARD_MAX_FILE_BYTES", DEFAULT_CLIPBOARD_MAX_FILE_BYTES
)
)
def clipboard_max_total_bytes() -> int:
return int(
os.environ.get(
"CLIPBOARD_MAX_TOTAL_BYTES", DEFAULT_CLIPBOARD_MAX_TOTAL_BYTES
)
)