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