init
This commit is contained in:
27
app/services/loader.py
Normal file
27
app/services/loader.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from app.models import AppConfig
|
||||
|
||||
_ENV_PATTERN = re.compile(r"\$\{([A-Z0-9_]+)\}")
|
||||
|
||||
|
||||
def _expand_env(value):
|
||||
if isinstance(value, str):
|
||||
return _ENV_PATTERN.sub(lambda m: os.getenv(m.group(1), ""), value)
|
||||
if isinstance(value, list):
|
||||
return [_expand_env(v) for v in value]
|
||||
if isinstance(value, dict):
|
||||
return {k: _expand_env(v) for k, v in value.items()}
|
||||
return value
|
||||
|
||||
|
||||
def load_app_config(path: Path) -> AppConfig:
|
||||
raw = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
|
||||
expanded = _expand_env(raw)
|
||||
return AppConfig.model_validate(expanded)
|
||||
Reference in New Issue
Block a user