service_man/api/infrastructure/dependencies/configs.py

53 lines
1.4 KiB
Python
Raw Permalink Normal View History

2024-03-31 21:07:59 +03:00
import os
from functools import lru_cache
2024-04-08 00:31:15 +03:00
from typing import Annotated
2024-03-31 21:07:59 +03:00
import yaml # type: ignore
2024-04-08 00:31:15 +03:00
from fastapi import Depends
2024-03-31 21:07:59 +03:00
2024-04-02 22:33:15 +03:00
from api.infrastructure.auth.jwt_settings import JwtSettings
2024-04-08 00:31:15 +03:00
from api.infrastructure.dependencies.stub import Stub
2024-03-31 21:07:59 +03:00
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
2024-04-08 00:31:15 +03:00
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:
2024-03-31 21:07:59 +03:00
config_data = yaml_loader(
file=os.getenv("CONFIG_PATH", "./config/api_config.yml"),
)
2024-04-08 00:31:15 +03:00
return JwtSettings(
secret=config_data["jwt"]["secret_key"],
expires_in=int(config_data["jwt"]["expires_in"]),
algorithm=config_data["jwt"]["algorithm"],
)
2024-03-31 21:07:59 +03:00
2024-04-08 00:31:15 +03:00
@lru_cache
def app_settings(
db_conf: Annotated[DBSettings, Depends(Stub(DBSettings))],
jwt_conf: Annotated[JwtSettings, Depends(Stub(JwtSettings))],
) -> Settings:
2024-03-31 21:07:59 +03:00
return Settings(
2024-04-08 00:31:15 +03:00
db=db_conf,
jwt=jwt_conf,
2024-03-31 21:07:59 +03:00
)