settings.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. from datetime import timedelta
  3. class Config:
  4. """Base configuration"""
  5. SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production')
  6. # JWT Configuration
  7. JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY', 'jwt-secret-key-change-in-production')
  8. JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=1)
  9. JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=7)
  10. # SQLAlchemy Configuration
  11. SQLALCHEMY_TRACK_MODIFICATIONS = False
  12. # Celery Configuration
  13. CELERY_BROKER_URL = os.environ.get('CELERY_BROKER_URL', 'redis://localhost:6379/0')
  14. CELERY_RESULT_BACKEND = os.environ.get('CELERY_RESULT_BACKEND', 'redis://localhost:6379/1')
  15. # File Storage
  16. UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER', 'uploads')
  17. REPORTS_FOLDER = os.environ.get('REPORTS_FOLDER', 'reports')
  18. MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16MB max file size
  19. # Encryption key for sensitive data
  20. ENCRYPTION_KEY = os.environ.get('ENCRYPTION_KEY', 'encryption-key-change-in-production')
  21. class DevelopmentConfig(Config):
  22. """Development configuration"""
  23. DEBUG = True
  24. SQLALCHEMY_DATABASE_URI = os.environ.get(
  25. 'DATABASE_URL',
  26. 'sqlite:///dev.db'
  27. )
  28. class TestingConfig(Config):
  29. """Testing configuration"""
  30. TESTING = True
  31. DEBUG = True
  32. SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
  33. # Use shorter token expiry for testing
  34. JWT_ACCESS_TOKEN_EXPIRES = timedelta(minutes=5)
  35. class ProductionConfig(Config):
  36. """Production configuration"""
  37. DEBUG = False
  38. SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
  39. # Ensure these are set in production
  40. @classmethod
  41. def init_app(cls, app):
  42. Config.init_app(app)
  43. # Validate required environment variables
  44. required_vars = ['SECRET_KEY', 'JWT_SECRET_KEY', 'DATABASE_URL', 'ENCRYPTION_KEY']
  45. missing = [var for var in required_vars if not os.environ.get(var)]
  46. if missing:
  47. raise ValueError(f"Missing required environment variables: {', '.join(missing)}")
  48. config = {
  49. 'development': DevelopmentConfig,
  50. 'testing': TestingConfig,
  51. 'production': ProductionConfig,
  52. 'default': DevelopmentConfig
  53. }