99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
"""Application configuration split by architecture component."""
|
|
|
|
import os
|
|
from functools import lru_cache
|
|
|
|
from pydantic import BaseModel, Field
|
|
from pydantic_settings import (
|
|
BaseSettings,
|
|
PydanticBaseSettingsSource,
|
|
SettingsConfigDict,
|
|
YamlConfigSettingsSource,
|
|
)
|
|
|
|
DEFAULT_CONFIG_FILE = "config.yaml"
|
|
CONFIG_FILE_ENV = "G2S_CONFIG_FILE"
|
|
|
|
|
|
class ControllerConfig(BaseModel):
|
|
api_prefix: str = "/api/v1"
|
|
request_id_header: str = "X-Request-ID"
|
|
|
|
|
|
class ServiceConfig(BaseModel):
|
|
provider_timeout_seconds: float = 10.0
|
|
max_parallel_providers: int = 8
|
|
|
|
|
|
class BusinessLogicConfig(BaseModel):
|
|
weight_round_scale: int = 2
|
|
|
|
|
|
class RepositoryConfig(BaseModel):
|
|
redis_dsn: str = "redis://localhost:6379/0"
|
|
price_cache_ttl_seconds: int = 900
|
|
|
|
|
|
class AdapterConfig(BaseModel):
|
|
cdek_base_url: str = "https://api.cdek.ru/v2"
|
|
cdek_client_id: str = ""
|
|
cdek_client_secret: str = ""
|
|
cdek_retry_attempts: int = Field(default=2, ge=0)
|
|
cdek_retry_backoff_seconds: float = Field(default=0.2, ge=0)
|
|
cdek_timeout_seconds: float = Field(default=10.0, gt=0)
|
|
cdek_cache_ttl_seconds: int = Field(default=900, gt=0)
|
|
|
|
|
|
class ObservabilityConfig(BaseModel):
|
|
service_name: str = "g2s-aggregator"
|
|
otlp_endpoint: str = "http://localhost:4317"
|
|
log_level: str = "INFO"
|
|
|
|
|
|
class AlertsConfig(BaseModel):
|
|
telegram_enabled: bool = False
|
|
telegram_bot_token: str | None = None
|
|
telegram_chat_id: str | None = None
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(
|
|
env_prefix="G2S_",
|
|
env_nested_delimiter="__",
|
|
extra="ignore",
|
|
)
|
|
|
|
controller: ControllerConfig = Field(default_factory=ControllerConfig)
|
|
service: ServiceConfig = Field(default_factory=ServiceConfig)
|
|
business_logic: BusinessLogicConfig = Field(default_factory=BusinessLogicConfig)
|
|
repository: RepositoryConfig = Field(default_factory=RepositoryConfig)
|
|
adapter: AdapterConfig = Field(default_factory=AdapterConfig)
|
|
observability: ObservabilityConfig = Field(default_factory=ObservabilityConfig)
|
|
alerts: AlertsConfig = Field(default_factory=AlertsConfig)
|
|
|
|
@classmethod
|
|
def settings_customise_sources(
|
|
cls,
|
|
settings_cls: type[BaseSettings],
|
|
init_settings: PydanticBaseSettingsSource,
|
|
env_settings: PydanticBaseSettingsSource,
|
|
dotenv_settings: PydanticBaseSettingsSource,
|
|
file_secret_settings: PydanticBaseSettingsSource,
|
|
) -> tuple[PydanticBaseSettingsSource, ...]:
|
|
yaml_source = YamlConfigSettingsSource(
|
|
settings_cls,
|
|
yaml_file=os.getenv(CONFIG_FILE_ENV, DEFAULT_CONFIG_FILE),
|
|
)
|
|
return (
|
|
init_settings,
|
|
env_settings,
|
|
dotenv_settings,
|
|
yaml_source,
|
|
file_secret_settings,
|
|
)
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def get_settings() -> Settings:
|
|
return Settings()
|