settings.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. from dotenv import load_dotenv
  3. import logging
  4. from httpx import get
  5. # Load environment variables from .env file
  6. load_dotenv()
  7. LOGS_FOLDER = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'logs')
  8. if not os.path.exists(LOGS_FOLDER):
  9. os.makedirs(LOGS_FOLDER)
  10. LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
  11. # Configure logging
  12. logging.basicConfig(
  13. level=getattr(logging, LOG_LEVEL),
  14. format='%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s',
  15. handlers=[
  16. logging.FileHandler(os.path.join(LOGS_FOLDER, 'app.log')),
  17. logging.StreamHandler()
  18. ]
  19. )
  20. # Configuration
  21. OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
  22. PORT = int(os.getenv("PORT", 6001))
  23. EXCLUDED_BEER_IDS = [14, 12, 11]
  24. # SECRET_KEY is crucial for signing session cookies.
  25. # Fallback to a default if not set, but warn that this is insecure for production.
  26. SECRET_KEY = os.getenv("SECRET_KEY", "your_very_very_secret_key_for_signing_cookies_python_v2")
  27. # Data paths
  28. BG_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),'data', 'llm_data.json')
  29. PRODUCTS_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)),'data', 'products.json')
  30. def validate_config():
  31. logger = logging.getLogger(__name__)
  32. """Validate configuration and show warnings"""
  33. if SECRET_KEY == "your_very_very_secret_key_for_signing_cookies_python_v2":
  34. logger.warning("Using default SECRET_KEY. Please set a strong SECRET_KEY in your .env file for production.")
  35. if not OPENAI_API_KEY:
  36. logger.critical("CRITICAL ERROR: OPENAI_API_KEY environment variable not set. The application will not work correctly.")
  37. return False
  38. return True