All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 56s
This is a FastAPI backend microservice template used with `copier` utility. Features of applied template are: - Configuration file processing logic - Metrics and tracing (both optional) configuration available - Debug endpoints - Database migration commands, prepared Alembic environment - Database usage example in ping_db endpoint - gitea sanity check pipeline
44 lines
1.2 KiB
Django/Jinja
44 lines
1.2 KiB
Django/Jinja
"""Database configuration class is defined here."""
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
from {{project_slug}}.utils.secrets import SecretStr
|
|
|
|
|
|
@dataclass
|
|
class DBConfig:
|
|
host: str
|
|
port: int
|
|
database: str
|
|
user: str
|
|
password: SecretStr
|
|
pool_size: int
|
|
|
|
def __post_init__(self):
|
|
self.password = SecretStr(self.password)
|
|
|
|
|
|
@dataclass
|
|
class MultipleDBsConfig:
|
|
master: DBConfig
|
|
replicas: list[DBConfig] | None = None
|
|
|
|
def __post_init__(self):
|
|
_dict_to_dataclass(self, "master", DBConfig)
|
|
if self.replicas is not None:
|
|
_list_dict_to_dataclasses(self, "replicas", DBConfig)
|
|
|
|
|
|
def _list_dict_to_dataclasses(config_entry: Any, field_name: str, need_type: type) -> None:
|
|
list_dict = getattr(config_entry, field_name)
|
|
for i in range(len(list_dict)): # pylint: disable=consider-using-enumerate
|
|
if isinstance(list_dict[i], dict):
|
|
list_dict[i] = need_type(**list_dict[i])
|
|
|
|
|
|
def _dict_to_dataclass(config_entry: Any, field_name: str, need_type: type) -> None:
|
|
value = getattr(config_entry, field_name)
|
|
if isinstance(value, dict):
|
|
setattr(config_entry, field_name, need_type(**value))
|