010 fix config
This commit is contained in:
+37
-8
@@ -1,7 +1,9 @@
|
||||
"""Application configuration split by architecture component."""
|
||||
|
||||
import os
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
from pydantic_settings import (
|
||||
@@ -12,7 +14,7 @@ from pydantic_settings import (
|
||||
)
|
||||
|
||||
DEFAULT_CONFIG_FILE = "config.yaml"
|
||||
CONFIG_FILE_ENV = "G2S_CONFIG_FILE"
|
||||
TEST_CONFIG_FILE = "config.test.yaml"
|
||||
|
||||
|
||||
class ControllerConfig(BaseModel):
|
||||
@@ -94,8 +96,6 @@ class AlertsConfig(BaseModel):
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
env_prefix="G2S_",
|
||||
env_nested_delimiter="__",
|
||||
extra="ignore",
|
||||
)
|
||||
|
||||
@@ -116,19 +116,48 @@ class Settings(BaseSettings):
|
||||
dotenv_settings: PydanticBaseSettingsSource,
|
||||
file_secret_settings: PydanticBaseSettingsSource,
|
||||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||
config_file = _resolve_runtime_config_file()
|
||||
yaml_source = YamlConfigSettingsSource(
|
||||
settings_cls,
|
||||
yaml_file=os.getenv(CONFIG_FILE_ENV, DEFAULT_CONFIG_FILE),
|
||||
yaml_file=config_file,
|
||||
)
|
||||
return (
|
||||
init_settings,
|
||||
env_settings,
|
||||
dotenv_settings,
|
||||
yaml_source,
|
||||
file_secret_settings,
|
||||
)
|
||||
|
||||
|
||||
class _RequiredYamlSections(BaseModel):
|
||||
controller: dict[str, Any]
|
||||
service: dict[str, Any]
|
||||
business_logic: dict[str, Any]
|
||||
repository: dict[str, Any]
|
||||
adapter: dict[str, Any]
|
||||
observability: dict[str, Any]
|
||||
alerts: dict[str, Any]
|
||||
|
||||
|
||||
def _resolve_runtime_config_file() -> str:
|
||||
test_config_path = Path(TEST_CONFIG_FILE)
|
||||
if "pytest" in sys.modules and test_config_path.is_file():
|
||||
return TEST_CONFIG_FILE
|
||||
return DEFAULT_CONFIG_FILE
|
||||
|
||||
|
||||
def _validate_required_yaml_sections() -> None:
|
||||
config_file = _resolve_runtime_config_file()
|
||||
config_path = Path(config_file)
|
||||
if not config_path.is_file():
|
||||
return
|
||||
|
||||
yaml_source = YamlConfigSettingsSource(Settings, yaml_file=config_file)
|
||||
yaml_data = yaml_source.yaml_data
|
||||
if not isinstance(yaml_data, dict):
|
||||
yaml_data = {}
|
||||
_RequiredYamlSections.model_validate(yaml_data)
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_settings() -> Settings:
|
||||
_validate_required_yaml_sections()
|
||||
return Settings()
|
||||
|
||||
Reference in New Issue
Block a user