Skip to content

Commit

Permalink
refactor: redis url as computed config attr
Browse files Browse the repository at this point in the history
  • Loading branch information
grillazz committed Mar 24, 2024
1 parent 96484e2 commit f7abc9f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 30 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f7abc9f

Please sign in to comment.