service_man/api/config.py

48 lines
925 B
Python
Raw Normal View History

2024-03-11 07:48:04 +03:00
import os
2024-03-29 00:29:35 +03:00
from dataclasses import dataclass
from functools import lru_cache
2024-03-11 07:48:04 +03:00
import yaml # type: ignore
2024-03-29 00:29:35 +03:00
@dataclass(frozen=True)
class DBSettings:
pg_user: str
pg_pass: str
pg_host: str
pg_port: int
pg_db: str
2024-03-11 07:48:04 +03:00
@property
2024-03-29 00:29:35 +03:00
def db_url(self) -> str:
2024-03-11 07:48:04 +03:00
return "postgresql+asyncpg://{}:{}@{}:{}/{}".format(
self.pg_user,
self.pg_pass,
self.pg_host,
self.pg_port,
self.pg_db,
)
2024-03-29 00:29:35 +03:00
@dataclass(frozen=True)
class RedisSettings:
redis_host: str
redis_port: int
2024-03-11 07:48:04 +03:00
2024-03-29 00:29:35 +03:00
@dataclass(frozen=True)
class Settings:
db: DBSettings
redis: RedisSettings
2024-03-11 07:48:04 +03:00
2024-03-29 00:29:35 +03:00
@lru_cache
2024-03-11 07:48:04 +03:00
def get_settings():
2024-03-29 00:29:35 +03:00
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"]),
)