This commit is contained in:
Piotr Dec 2025-10-07 00:06:04 +02:00
parent 2cde0de07a
commit b18488a6d3
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
4 changed files with 38 additions and 27 deletions

1
app/config/__init__.py Normal file
View file

@ -0,0 +1 @@
from .settings import get_settings

28
app/config/settings.py Normal file
View file

@ -0,0 +1,28 @@
from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import BaseModel
from pathlib import Path
import yaml
class AppConfig(BaseModel):
host: str = "127.0.0.1"
port: int = 8000
reload: bool = True
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="KARL_", env_nested_delimiter="__")
app: AppConfig = AppConfig()
@classmethod
def from_yaml(cls, path: Path | str = "config/config.yaml") -> "Settings":
p = Path(path)
data = {}
if p.exists():
with p.open("r", encoding="utf-8") as fh:
data = yaml.safe_load(fh) or {}
return cls(**data)
@lru_cache
def get_settings() -> Settings:
return Settings.from_yaml()