46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import os
|
|||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
BASE_DIR = Path(__file__).resolve().parent
|
||
|
|
SECRET_FILE = BASE_DIR / ".secret_key"
|
||
|
|
|
||
|
|
DEFAULT_SENSORS_PORT = 8001
|
||
|
|
|
||
|
|
|
||
|
|
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}"
|