Files
mtusi2026labs/src/messenger/shared/config.py
T
ArtyomandClaude Opus 5.5 540fcb35ba Carry over the lab 4 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6 and RustFS configuration, migrations, hooks, evidence and
scenario scripts and the CI job from the lab 4 solution commit d2744bc,
merge the lab 4 Makefile targets into the lab 5 skeleton Makefile
without touching its targets, and append the lab 4 solution variables
to the skeleton .env.example. The package and the application follow
the contract version 1.5.0, and the image and the throwaway test
containers are tagged lab5 so they do not collide with a lab 4 stand on
the same host.

This is the starting point of the lab 5 cycle: lint, own tests and the
offline skeleton check pass before any lab 5 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 00:34:03 +03:00

50 lines
1.8 KiB
Python

import os
import socket
from pathlib import Path
from typing import Any
from pydantic import Field, SecretStr
from pydantic.fields import FieldInfo
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict
CONFIG = SettingsConfigDict(extra="ignore", frozen=True)
class FileVariables(PydanticBaseSettingsSource):
def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str, bool]:
path = os.environ.get(f"{field_name.upper()}_FILE")
return (Path(path).read_text().strip() if path else None), field_name, False
def __call__(self) -> dict[str, Any]:
found = {
name: self.get_field_value(field, name)[0]
for name, field in self.settings_cls.model_fields.items()
}
return {name: value for name, value in found.items() if value is not None}
class Settings(BaseSettings):
model_config = CONFIG
@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, ...]:
return init_settings, env_settings, FileVariables(settings_cls)
class PlatformSettings(Settings):
log_level: str = "INFO"
instance_id: str = Field(default_factory=socket.gethostname, min_length=1)
event_queue_max: int = Field(default=10_000, ge=16)
database_url: SecretStr
db_pool_max_size: int = Field(default=8, ge=1, le=50)
db_connect_timeout_seconds: float = Field(default=1.0, gt=0, le=30)
db_acquire_timeout_seconds: float = Field(default=1.0, gt=0, le=30)
db_command_timeout_seconds: float = Field(default=2.0, gt=0, le=60)