28 lines
735 B
Python
28 lines
735 B
Python
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()
|