config.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import logging
  2. import os
  3. from pathlib import Path
  4. from pydantic_settings import BaseSettings
  5. # Application version - single source of truth
  6. APP_VERSION = "0.2.4b3"
  7. GITHUB_REPO = "maziggy/bambuddy"
  8. BUG_REPORT_RELAY_URL = os.environ.get("BUG_REPORT_RELAY_URL", "https://bambuddy.cool/api/bug-report")
  9. # App directory - where the application is installed (for static files)
  10. _app_dir = Path(__file__).resolve().parent.parent.parent.parent
  11. # Data directory - for persistent data (database, archives)
  12. # Use DATA_DIR env var if set (Docker), otherwise use project root (local dev)
  13. _data_dir_env = os.environ.get("DATA_DIR")
  14. _data_dir = Path(_data_dir_env) if _data_dir_env else _app_dir
  15. # Plate calibration directory - special handling to maintain backwards compatibility
  16. # Docker: DATA_DIR/plate_calibration (e.g., /data/plate_calibration)
  17. # Local dev: project_root/data/plate_calibration (original location)
  18. _plate_cal_dir = Path(_data_dir_env) / "plate_calibration" if _data_dir_env else _app_dir / "data" / "plate_calibration"
  19. # Log directory - use LOG_DIR env var if set, otherwise use app_dir/logs
  20. _log_dir_env = os.environ.get("LOG_DIR")
  21. _log_dir = Path(_log_dir_env) if _log_dir_env else _app_dir / "logs"
  22. def _migrate_database() -> Path:
  23. """Migrate database from old name to new name if needed."""
  24. old_db = _data_dir / "bambutrack.db"
  25. new_db = _data_dir / "bambuddy.db"
  26. # If old database exists and new one doesn't, rename it
  27. if old_db.exists() and not new_db.exists():
  28. try:
  29. old_db.rename(new_db)
  30. logging.info("Migrated database: %s -> %s", old_db, new_db)
  31. except Exception as e:
  32. logging.warning("Could not migrate database: %s. Using old location.", e)
  33. return old_db
  34. # If old database exists (and new one now exists too), it was migrated
  35. # If only new exists, use new
  36. # If neither exists, use new (will be created)
  37. return new_db if new_db.exists() or not old_db.exists() else old_db
  38. # External DATABASE_URL takes priority (PostgreSQL support)
  39. _external_db_url = os.environ.get("DATABASE_URL")
  40. # Determine database path (handles migration) — only used for SQLite
  41. _db_path = _migrate_database() if not _external_db_url else None
  42. class Settings(BaseSettings):
  43. app_name: str = "Bambuddy"
  44. debug: bool = False # Default to production mode
  45. # Paths
  46. base_dir: Path = _data_dir # For backwards compatibility
  47. # `app_dir` is where the source code is checked out — distinct from `base_dir`
  48. # on native installs where DATA_DIR is set to a sibling like INSTALL_PATH/data.
  49. # Use this when you need the working tree (requirements.txt, frontend/, etc.)
  50. # rather than the data dir. On Docker / local dev where DATA_DIR is unset,
  51. # app_dir == base_dir.
  52. app_dir: Path = _app_dir
  53. archive_dir: Path = _data_dir / "archive"
  54. plate_calibration_dir: Path = _plate_cal_dir # Plate detection references
  55. static_dir: Path = _app_dir / "static" # Static files are part of app, not data
  56. log_dir: Path = _log_dir
  57. database_url: str = _external_db_url or f"sqlite+aiosqlite:///{_db_path}"
  58. # Logging
  59. log_level: str = "INFO" # Override with LOG_LEVEL env var or DEBUG=true
  60. log_to_file: bool = True # Set to false to disable file logging
  61. # API
  62. api_prefix: str = "/api/v1"
  63. # Slicer API sidecars. Defaults match the docker-compose.yml ports in the
  64. # orca-slicer-api fork (https://github.com/maziggy/orca-slicer-api):
  65. # OrcaSlicer → port 3003 (default profile)
  66. # BambuStudio → port 3001 (built locally via Dockerfile.bambu-studio)
  67. # The slice route picks which one based on the user's preferred_slicer
  68. # setting.
  69. slicer_api_url: str = "http://localhost:3003"
  70. bambu_studio_api_url: str = "http://localhost:3001"
  71. class Config:
  72. env_file = ".env"
  73. env_file_encoding = "utf-8"
  74. settings = Settings()
  75. # Ensure directories exist
  76. settings.archive_dir.mkdir(parents=True, exist_ok=True)
  77. settings.plate_calibration_dir.mkdir(parents=True, exist_ok=True)
  78. settings.static_dir.mkdir(exist_ok=True)
  79. if settings.log_to_file:
  80. settings.log_dir.mkdir(exist_ok=True)