001 Project structure
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""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_timeout_seconds: float = 10.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()
|
||||
Reference in New Issue
Block a user