- Add production.py with security hardening and performance optimizations - Required environment variables for security (JWT, passwords, database) - Database connection pooling for PostgreSQL/MySQL - Security configurations and allowed hosts - Production logging and rotation settings - API rate limiting and performance tuning - Add development.py with relaxed settings for local development - Defaults for development (SQLite, debug logging, auto-reload) - Higher rate limits and longer session timeouts - Dev credentials for easy local setup - Development database defaults - Add environment configuration loader (__init__.py) - Automatic environment detection - Factory functions for lazy loading settings - Proper environment validation - Add startup scripts (start.sh) - Bash script for starting application in any environment - Conda environment validation - Automatic directory creation - Environment file generation - Database initialization - Development vs production startup modes - Add setup script (setup.py) - Python setup automation for environment initialization - Dependency installation - Environment file generation - Database initialization - Comprehensive validation and error handling - Update requirements.txt with psutil dependency All configurations follow project coding standards and include comprehensive documentation, type hints, and error handling.
70 lines
1.9 KiB
Python
70 lines
1.9 KiB
Python
"""
|
|
Environment configuration loader for Aniworld application.
|
|
|
|
This module provides unified configuration loading based on the environment
|
|
(development, production, or testing). It automatically selects the appropriate
|
|
settings configuration based on the ENVIRONMENT variable.
|
|
"""
|
|
|
|
import os
|
|
from typing import Union
|
|
|
|
from .development import DevelopmentSettings, get_development_settings
|
|
from .production import ProductionSettings, get_production_settings
|
|
|
|
# Environment options
|
|
ENVIRONMENT = os.getenv("ENVIRONMENT", "development").lower()
|
|
|
|
# Valid environment values
|
|
VALID_ENVIRONMENTS = {"development", "production", "testing"}
|
|
|
|
if ENVIRONMENT not in VALID_ENVIRONMENTS:
|
|
raise ValueError(
|
|
f"Invalid ENVIRONMENT '{ENVIRONMENT}'. "
|
|
f"Must be one of: {VALID_ENVIRONMENTS}"
|
|
)
|
|
|
|
|
|
def get_settings() -> Union[DevelopmentSettings, ProductionSettings]:
|
|
"""
|
|
Get environment-specific settings.
|
|
|
|
Returns:
|
|
DevelopmentSettings: If ENVIRONMENT is 'development' or 'testing'
|
|
ProductionSettings: If ENVIRONMENT is 'production'
|
|
|
|
Raises:
|
|
ValueError: If ENVIRONMENT is not valid
|
|
|
|
Example:
|
|
>>> settings = get_settings()
|
|
>>> print(settings.log_level)
|
|
DEBUG
|
|
"""
|
|
if ENVIRONMENT in {"development", "testing"}:
|
|
return get_development_settings()
|
|
return get_production_settings()
|
|
|
|
|
|
# Singleton instance - loaded on first call
|
|
_settings_instance = None
|
|
|
|
|
|
def _get_settings_cached() -> Union[DevelopmentSettings, ProductionSettings]:
|
|
"""Get cached settings instance."""
|
|
global _settings_instance
|
|
if _settings_instance is None:
|
|
_settings_instance = get_settings()
|
|
return _settings_instance
|
|
|
|
|
|
# Re-export for convenience
|
|
__all__ = [
|
|
"get_settings",
|
|
"ENVIRONMENT",
|
|
"DevelopmentSettings",
|
|
"ProductionSettings",
|
|
"get_development_settings",
|
|
"get_production_settings",
|
|
]
|