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

1
.env
View File

@ -19,7 +19,6 @@ POSTGRES_PASSWORD=secret
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=2
REDIS_URL="redis://${REDIS_HOST}:${REDIS_PORT}/${REDIS_DB}"
JWT_EXPIRE=3600
JWT_ALGORITHM=HS256

View File

@ -26,7 +26,6 @@ jobs:
REDIS_HOST: 127.0.0.1
REDIS_PORT: 6379
REDIS_DB: 2
REDIS_URL: redis://127.0.0.1:6379/2
JWT_EXPIRE: 3600
JWT_ALGORITHM: HS256

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: