refactor: redis url as computed config attr

This commit is contained in:
Jakub Miazek
2024-03-24 11:05:22 +01:00
parent 96484e20a3
commit f7abc9f3b7
3 changed files with 30 additions and 3 deletions

View File

@@ -11,15 +11,44 @@ class Settings(BaseSettings):
env_ignore_empty=True,
extra="ignore"
)
redis_url: RedisDsn = os.getenv("REDIS_URL")
jwt_algorithm: str = os.getenv("JWT_ALGORITHM")
jwt_expire: int = os.getenv("JWT_EXPIRE")
REDIS_HOST: str
REDIS_PORT: int
REDIS_DB: str
JWT_ALGORITHM: str
JWT_EXPIRE: int
SQL_USER: str
SQL_PASS: str
SQL_HOST: str
SQL_DB: str
@computed_field
@property
def redis_url(self) -> RedisDsn:
"""
This is a computed field that generates a RedisDsn URL for redis-py.
The URL is built using the MultiHostUrl.build method, which takes the following parameters:
- scheme: The scheme of the URL. In this case, it is "redis".
- host: The host of the Redis database, retrieved from the REDIS_HOST environment variable.
- port: The port of the Redis database, retrieved from the REDIS_PORT environment variable.
- path: The path of the Redis database, retrieved from the REDIS_DB environment variable.
Returns:
RedisDsn: The constructed RedisDsn URL for redis-py.
"""
return MultiHostUrl.build(
scheme="redis",
host=self.REDIS_HOST,
port=self.REDIS_PORT,
path=self.REDIS_DB,
)
@computed_field
@property
def asyncpg_url(self) -> PostgresDsn: