service_man/api/infrastructure/dependencies/configs.py

53 lines
1.4 KiB
Python

import os
from functools import lru_cache
from typing import Annotated
import yaml # type: ignore
from fastapi import Depends
from api.infrastructure.auth.jwt_settings import JwtSettings
from api.infrastructure.dependencies.stub import Stub
from api.infrastructure.persistence.db_setings import DBSettings
from api.infrastructure.settings import Settings
def yaml_loader(file: str) -> dict[str, dict[str, str]]:
with open(file) as f:
yaml_data: dict = yaml.safe_load(f)
return yaml_data
def get_db_settings() -> DBSettings:
config_data = yaml_loader(
file=os.getenv("CONFIG_PATH", "./config/api_config.yml"),
)
return DBSettings(
pg_user=config_data["db"]["user"],
pg_pass=config_data["db"]["password"],
pg_host=config_data["db"]["host"],
pg_port=int(config_data["db"]["port"]),
pg_db=config_data["db"]["database"],
)
def get_jwt_settings() -> JwtSettings:
config_data = yaml_loader(
file=os.getenv("CONFIG_PATH", "./config/api_config.yml"),
)
return JwtSettings(
secret=config_data["jwt"]["secret_key"],
expires_in=int(config_data["jwt"]["expires_in"]),
algorithm=config_data["jwt"]["algorithm"],
)
@lru_cache
def app_settings(
db_conf: Annotated[DBSettings, Depends(Stub(DBSettings))],
jwt_conf: Annotated[JwtSettings, Depends(Stub(JwtSettings))],
) -> Settings:
return Settings(
db=db_conf,
jwt=jwt_conf,
)