diff --git a/mapping_provider/__init__.py b/mapping_provider/__init__.py index 32f69209e94a8951f8fde76596c2988d9c71d24c..2fdc7a3be0898c5de7b7d5601201ce3d69a8f46e 100644 --- a/mapping_provider/__init__.py +++ b/mapping_provider/__init__.py @@ -1,11 +1,9 @@ -"""Initializes the FastAPI application.""" -import os +"""Create a FastAPI application for the mapping provider.""" from fastapi import FastAPI -from mapping_provider import config from mapping_provider.api.common import router as version_router - +from mapping_provider.api.network_graph import router as graph_router def create_app() -> FastAPI: """Create a FastAPI application.""" @@ -14,9 +12,11 @@ def create_app() -> FastAPI: description="Mapping provider endpoints for GÉANT maps", ) - config_file = os.environ.get("CONFIG_FILE_NAME", "config.json") - with config_file.open() as f: - app.state.config = config.load(f) + # Force configuration to be loaded at startup to avoid issues with missing config + from mapping_provider.dependencies import load_config + _ = load_config() app.include_router(version_router) - return app + app.include_router(graph_router) + + return app \ No newline at end of file diff --git a/mapping_provider/dependencies.py b/mapping_provider/dependencies.py new file mode 100644 index 0000000000000000000000000000000000000000..a776e79aabf28891b661a147ce5e337b636cbe88 --- /dev/null +++ b/mapping_provider/dependencies.py @@ -0,0 +1,20 @@ +"""FastAPI project dependencies.""" + +import os +from functools import lru_cache +from typing import Annotated + +from fastapi import Depends +from mapping_provider import config + + +@lru_cache() +def load_config() -> dict: + """Load and cache the application configuration.""" + config_file = os.environ.get("CONFIG_FILE_NAME", "config.json") + with open(config_file) as f: + return config.load(f) + + +# Dependency for injecting config into routes/services +config_dep = Annotated[dict, Depends(load_config)] \ No newline at end of file diff --git a/mapping_provider/main.py b/mapping_provider/main.py deleted file mode 100644 index dfcd3b48ae2aeb815081e1cf6fe07e90020d8eff..0000000000000000000000000000000000000000 --- a/mapping_provider/main.py +++ /dev/null @@ -1,6 +0,0 @@ -"""The main module that runs the application.""" - -from mapping_provider import create_app - -app = create_app() -