Skip to content

Commit

Permalink
Support username and password in url (#5)
Browse files Browse the repository at this point in the history
* Support username and password in url

* Update readme
  • Loading branch information
Wh1isper authored Jun 6, 2024
1 parent eadb1fa commit 09983a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ async def main():
db=int(os.getenv("REDIS_DB", 0)),
cluster=bool(os.getenv("REDIS_CLUSTER", "false") in ["True", "true", "1"]),
tls=bool(os.getenv("REDIS_TLS", "false") in ["True", "true", "1"]),
username=os.getenv("REDIS_USERNAME", ""),
password=os.getenv("REDIS_PASSWORD", ""),
)
async with get_redis_client(redis_url) as async_redis_client:
await Producer(async_redis_client).run_job("echo", ["hello"])
Expand Down Expand Up @@ -80,6 +82,8 @@ async def main():
db=int(os.getenv("REDIS_DB", 0)),
cluster=bool(os.getenv("REDIS_CLUSTER", "false") in ["True", "true", "1"]),
tls=bool(os.getenv("REDIS_TLS", "false") in ["True", "true", "1"]),
username=os.getenv("REDIS_USERNAME", ""),
password=os.getenv("REDIS_PASSWORD", ""),
)
async with get_redis_client(redis_url) as async_redis_client:
daemon = Daemon(Consumer(async_redis_client, echo))
Expand Down
5 changes: 5 additions & 0 deletions brq/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ def get_redis_url(
db: int = 0,
cluster: bool = False,
tls: bool = False,
username: str = "",
password: str = "",
):
if not cluster:
url = f"{host}:{port}/{db}"
else:
logger.debug("Cluster mode, no need to specify db")
url = f"{host}:{port}"

if username or password:
url = f"{username}:{password}@{url}"

if tls:
return f"rediss://{url}"
else:
Expand Down

0 comments on commit 09983a2

Please sign in to comment.