Skip to content

Commit f7abc9f

Browse files
committed
refactor: redis url as computed config attr
1 parent 96484e2 commit f7abc9f

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ POSTGRES_PASSWORD=secret
1919
REDIS_HOST=redis
2020
REDIS_PORT=6379
2121
REDIS_DB=2
22-
REDIS_URL="redis://${REDIS_HOST}:${REDIS_PORT}/${REDIS_DB}"
2322

2423
JWT_EXPIRE=3600
2524
JWT_ALGORITHM=HS256

.github/workflows/build-and-test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ jobs:
2626
REDIS_HOST: 127.0.0.1
2727
REDIS_PORT: 6379
2828
REDIS_DB: 2
29-
REDIS_URL: redis://127.0.0.1:6379/2
3029
JWT_EXPIRE: 3600
3130
JWT_ALGORITHM: HS256
3231

app/config.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,44 @@ class Settings(BaseSettings):
1111
env_ignore_empty=True,
1212
extra="ignore"
1313
)
14-
redis_url: RedisDsn = os.getenv("REDIS_URL")
1514
jwt_algorithm: str = os.getenv("JWT_ALGORITHM")
1615
jwt_expire: int = os.getenv("JWT_EXPIRE")
1716

17+
REDIS_HOST: str
18+
REDIS_PORT: int
19+
REDIS_DB: str
20+
21+
JWT_ALGORITHM: str
22+
JWT_EXPIRE: int
23+
1824
SQL_USER: str
1925
SQL_PASS: str
2026
SQL_HOST: str
2127
SQL_DB: str
2228

29+
@computed_field
30+
@property
31+
def redis_url(self) -> RedisDsn:
32+
"""
33+
This is a computed field that generates a RedisDsn URL for redis-py.
34+
35+
The URL is built using the MultiHostUrl.build method, which takes the following parameters:
36+
- scheme: The scheme of the URL. In this case, it is "redis".
37+
- host: The host of the Redis database, retrieved from the REDIS_HOST environment variable.
38+
- port: The port of the Redis database, retrieved from the REDIS_PORT environment variable.
39+
- path: The path of the Redis database, retrieved from the REDIS_DB environment variable.
40+
41+
Returns:
42+
RedisDsn: The constructed RedisDsn URL for redis-py.
43+
"""
44+
return MultiHostUrl.build(
45+
scheme="redis",
46+
host=self.REDIS_HOST,
47+
port=self.REDIS_PORT,
48+
path=self.REDIS_DB,
49+
50+
)
51+
2352
@computed_field
2453
@property
2554
def asyncpg_url(self) -> PostgresDsn:

0 commit comments

Comments
 (0)