feat: add redis password
This commit is contained in:
22
src/infrastructure/cache/client.py
vendored
22
src/infrastructure/cache/client.py
vendored
@@ -4,13 +4,15 @@ from src.infrastructure.config import settings
|
|||||||
|
|
||||||
|
|
||||||
def create_redis_client() -> Redis:
|
def create_redis_client() -> Redis:
|
||||||
return redis.from_url(
|
kw = {
|
||||||
settings.REDIS_URL,
|
'max_connections': 50,
|
||||||
max_connections=50,
|
'decode_responses': True,
|
||||||
decode_responses=True,
|
'socket_timeout': 5,
|
||||||
socket_timeout=5,
|
'socket_connect_timeout': 5,
|
||||||
socket_connect_timeout=5,
|
'health_check_interval': 30,
|
||||||
health_check_interval=30,
|
'retry_on_timeout': True,
|
||||||
retry_on_timeout=True,
|
'socket_keepalive': True,
|
||||||
socket_keepalive=True,
|
}
|
||||||
)
|
if settings.REDIS_PASSWORD:
|
||||||
|
kw['password'] = settings.REDIS_PASSWORD
|
||||||
|
return redis.from_url(settings.REDIS_URL, **kw)
|
||||||
@@ -104,6 +104,13 @@ class Settings(BaseSettings):
|
|||||||
return None
|
return None
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
@field_validator('REDIS_PASSWORD', mode='before')
|
||||||
|
@classmethod
|
||||||
|
def empty_redis_password_to_none(cls, v):
|
||||||
|
if v is None or (isinstance(v, str) and not v.strip()):
|
||||||
|
return None
|
||||||
|
return v
|
||||||
|
|
||||||
model_config = SettingsConfigDict(
|
model_config = SettingsConfigDict(
|
||||||
env_file='.env',
|
env_file='.env',
|
||||||
env_file_encoding='utf-8',
|
env_file_encoding='utf-8',
|
||||||
@@ -215,6 +222,26 @@ class Settings(BaseSettings):
|
|||||||
rb_set('password', 'RABBIT_PASSWORD')
|
rb_set('password', 'RABBIT_PASSWORD')
|
||||||
rb_set('vhost', 'RABBIT_VHOST')
|
rb_set('vhost', 'RABBIT_VHOST')
|
||||||
|
|
||||||
|
redis_secret = read_secret_optional('redis')
|
||||||
|
if redis_secret:
|
||||||
|
rd_ci = {str(k).lower(): v for k, v in redis_secret.items()}
|
||||||
|
|
||||||
|
def rd_set(field: str, env_key: str, *, as_int: bool = False) -> None:
|
||||||
|
v = rd_ci.get(field)
|
||||||
|
if v is None:
|
||||||
|
return
|
||||||
|
if isinstance(v, str) and not v.strip():
|
||||||
|
return
|
||||||
|
if as_int:
|
||||||
|
data[env_key] = int(v)
|
||||||
|
else:
|
||||||
|
data[env_key] = str(v).strip()
|
||||||
|
|
||||||
|
rd_set('host', 'REDIS_HOST')
|
||||||
|
rd_set('port', 'REDIS_PORT', as_int=True)
|
||||||
|
rd_set('password', 'REDIS_PASSWORD')
|
||||||
|
rd_set('db', 'REDIS_DB', as_int=True)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
@@ -231,8 +258,7 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def REDIS_URL(self) -> str:
|
def REDIS_URL(self) -> str:
|
||||||
auth = f":{self.REDIS_PASSWORD}@" if self.REDIS_PASSWORD else ""
|
return f'redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}'
|
||||||
return f"redis://{auth}{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def RABBIT_URL(self) -> str:
|
def RABBIT_URL(self) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user