""" Central configuration for the Garmin Health Lab Flask backend. Reads settings from a `.env` file (backend/.env) and the process environment. The same code runs against SQLite (local dev) or MariaDB (NAS production) by switching DB_TYPE — business code never branches on the backend. """ import os from dotenv import load_dotenv # Load .env from the backend directory (falls back to cwd / parent search). _BACKEND_DIR = os.path.dirname(os.path.abspath(__file__)) _ENV_PATH = os.path.join(_BACKEND_DIR, ".env") if os.path.exists(_ENV_PATH): load_dotenv(_ENV_PATH) else: load_dotenv() # walk up from cwd # --- Database selection ----------------------------------------------------- DB_TYPE = (os.environ.get("DB_TYPE") or "sqlite").lower() # SQLite (default, zero-config local development) SQLITE_PATH = os.environ.get("DATABASE_PATH") or os.path.join( _BACKEND_DIR, "data", "health.db" ) # MariaDB (production, runs on the NAS) MARIADB_SOCKET = os.environ.get("MARIADB_SOCKET") or "" MARIADB_HOST = os.environ.get("MARIADB_HOST") or "127.0.0.1" MARIADB_PORT = int(os.environ.get("MARIADB_PORT") or 3306) MARIADB_USER = os.environ.get("MARIADB_USER") or "root" MARIADB_PASSWORD = os.environ.get("MARIADB_PASSWORD") or "" MARIADB_DATABASE = os.environ.get("MARIADB_DATABASE") or "garmin_health_lab" # --- Auth ------------------------------------------------------------------- JWT_SECRET = os.environ.get("JWT_SECRET") or "dev_secret_change_me" JWT_EXPIRY_DAYS = int(os.environ.get("JWT_EXPIRY_DAYS") or 7) # Who may create an account. # "auto" - only while no user exists yet (first-run setup, then closed). # "true" - always open. # "false" - never; accounts must be created out of band. # "auto" is the default because this deployment is reachable from the public # internet, where an open registration endpoint would let anyone create an # account and start pulling health data. ALLOW_REGISTRATION = (os.environ.get("ALLOW_REGISTRATION") or "auto").lower() # --- Static UI -------------------------------------------------------------- # Directory holding the built React app. When set and populated, the Flask # process serves the UI too, so a deployment is one port and one service. STATIC_DIR = os.environ.get("STATIC_DIR") or os.path.join(_BACKEND_DIR, "static") # --- Server ----------------------------------------------------------------- # BACKEND_PORT wins over PORT: `PORT` is set by many dev tools and PaaS # runtimes for the *frontend*, and letting it through made Flask seize the # React dev server's port during `npm run dev`. PORT = int(os.environ.get("BACKEND_PORT") or os.environ.get("PORT") or 5000) # Comma-separated list of allowed front-end origins (CORS). _CORS_RAW = os.environ.get("CORS_ORIGIN") or "http://localhost:3000,http://localhost:5173" CORS_ORIGINS = [o.strip() for o in _CORS_RAW.split(",") if o.strip()]