File size: 1,739 Bytes
3ec4ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Module loader utilities."""

import logging
import uvicorn
from typing import Any, Optional

from src.modules.api.app import app as api_app
from src.modules.api.core.config import settings

logger = logging.getLogger(__name__)

class ModuleLoader:
    """Utility class for loading and running modules."""
    
    @staticmethod
    def load_api(
        host: str = "0.0.0.0",
        port: int = 8000,
        reload: bool = False,
        **kwargs: Any
    ) -> None:
        """Load and run the API module.
        
        Args:
            host: Host to bind the server to
            port: Port to bind the server to
            reload: Enable auto-reload for development
            **kwargs: Additional uvicorn configuration
        """
        logger.info(f"Starting API server on {host}:{port}")
        logger.debug(f"API Settings: {settings.dict()}")
        
        config = uvicorn.Config(
            api_app,
            host=host,
            port=port,
            reload=reload,
            log_level="debug" if settings.debug else "info",
            **kwargs
        )
        
        server = uvicorn.Server(config)
        server.run()

    @classmethod
    def load_module(
        cls,
        module_name: str,
        **kwargs: Any
    ) -> Optional[Any]:
        """Generic module loader.
        
        Args:
            module_name: Name of the module to load
            **kwargs: Module-specific configuration
            
        Returns:
            Optional[Any]: Module instance if applicable
        """
        if module_name == "api":
            cls.load_api(**kwargs)
            return api_app
        else:
            logger.error(f"Unknown module: {module_name}")
            return None