service_man/api/config.py

48 lines
925 B
Python

import os
from dataclasses import dataclass
from functools import lru_cache
import yaml # type: ignore
@dataclass(frozen=True)
class DBSettings:
pg_user: str
pg_pass: str
pg_host: str
pg_port: int
pg_db: str
@property
def db_url(self) -> str:
return "postgresql+asyncpg://{}:{}@{}:{}/{}".format(
self.pg_user,
self.pg_pass,
self.pg_host,
self.pg_port,
self.pg_db,
)
@dataclass(frozen=True)
class RedisSettings:
redis_host: str
redis_port: int
@dataclass(frozen=True)
class Settings:
db: DBSettings
redis: RedisSettings
@lru_cache
def get_settings():
with open(os.getenv("CONFIG_PATH", "./config/api_config.yml")) as f:
config_data: dict = yaml.safe_load(f)
return Settings(
db=DBSettings(**config_data["db"]),
redis=RedisSettings(**config_data["redis"]),
)