33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
import tomllib
|
|
from typing import Optional, Type, Dict, TypeVar
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class ConfigModel(BaseModel):
|
|
pass
|
|
|
|
|
|
# APP_ENV (backend 와 동일 규약)
|
|
# local : 로컬 환경(개인 pc) / dev : 개발환경 / prod : 서비스 환경
|
|
# 실행 시: export APP_ENV=dev (linux) / set APP_ENV=dev (windows)
|
|
class Configs:
|
|
ConfigType = TypeVar("ConfigType", bound=ConfigModel)
|
|
|
|
def __init__(self, file_path: str):
|
|
self._settings: Dict[Type["Configs.ConfigType"], "Configs.ConfigType"] = self._load_settings_from_toml(file_path)
|
|
|
|
def _load_settings_from_toml(self, file_path: str) -> Dict[Type[ConfigType], ConfigType]:
|
|
with open(file_path, "rb") as f:
|
|
toml_content = tomllib.load(f)
|
|
|
|
config_subclasses = ConfigModel.__subclasses__()
|
|
configs = {
|
|
config_class: config_class.model_validate(toml_content[config_class.__name__])
|
|
for config_class in config_subclasses
|
|
if config_class.__name__ in toml_content
|
|
}
|
|
return configs
|
|
|
|
def get(self, config_class: Type[ConfigType]) -> Optional[ConfigType]:
|
|
return self._settings.get(config_class)
|