Pydantic
This commit is contained in:
parent
2cde0de07a
commit
b18488a6d3
4 changed files with 38 additions and 27 deletions
28
app/config/settings.py
Normal file
28
app/config/settings.py
Normal 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue