Skip to content

Commit

Permalink
feat: Add support for env config of Redis username and password
Browse files Browse the repository at this point in the history
  • Loading branch information
danh91 committed May 24, 2024
1 parent 769c841 commit bd3f2d4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ WORKER_IMMEDIATE_MODE=True
# EMAIL_USE_TLS=false
# EMAIL_HOST=maildev
# [email protected]

## Redis config
# REDIS_HOST=localhost
# REDIS_PORT=6379
# REDIS_PASSWORD=redis
5 changes: 4 additions & 1 deletion apps/api/karrio/server/settings/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
CACHE_TTL = 60 * 15
REDIS_HOST = config("REDIS_HOST", default=None)
REDIS_PORT = config("REDIS_PORT", default=None)
REDIS_PASSWORD = config("REDIS_PASSWORD", default=None)
REDIS_USERNAME = config("REDIS_USERNAME", default="default")

# karrio server caching setup
if REDIS_HOST is not None:
HEALTH_CHECK_APPS += ["health_check.contrib.redis"]
INSTALLED_APPS += ["health_check.contrib.redis"]
REDIS_AUTH = f"{REDIS_USERNAME}:{REDIS_PASSWORD}@" if REDIS_PASSWORD else ""

REDIS_CONNECTION_URL = f'redis://{REDIS_HOST}:{REDIS_PORT or "6379"}/1'
REDIS_CONNECTION_URL = f'redis://${REDIS_AUTH}{REDIS_HOST}:{REDIS_PORT or "6379"}/1'
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
Expand Down
12 changes: 9 additions & 3 deletions apps/api/karrio/server/settings/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@

REDIS_HOST = decouple.config("REDIS_HOST", default=None)
REDIS_PORT = decouple.config("REDIS_PORT", default=None)
REDIS_PASSWORD = decouple.config("REDIS_PASSWORD", default=None)
REDIS_USERNAME = decouple.config("REDIS_USERNAME", default="default")


# Use redis if available
if REDIS_HOST is not None:
pool = redis.ConnectionPool(
host=REDIS_HOST, port=REDIS_PORT or "6379", max_connections=20
host=REDIS_HOST,
port=REDIS_PORT or "6379",
max_connections=20,
**({"password": REDIS_PASSWORD} if REDIS_PASSWORD else {}),
**({"username": REDIS_USERNAME} if REDIS_USERNAME else {}),
)
HUEY = huey.RedisHuey(
"default",
connection_pool=pool,
**({"immediate": WORKER_IMMEDIATE_MODE} if WORKER_IMMEDIATE_MODE else {})
**({"immediate": WORKER_IMMEDIATE_MODE} if WORKER_IMMEDIATE_MODE else {}),
)

else:
Expand All @@ -43,5 +49,5 @@
HUEY = huey.SqliteHuey(
name="default",
filename=WORKER_DB_FILE_NAME,
**({"immediate": WORKER_IMMEDIATE_MODE} if WORKER_IMMEDIATE_MODE else {})
**({"immediate": WORKER_IMMEDIATE_MODE} if WORKER_IMMEDIATE_MODE else {}),
)
7 changes: 2 additions & 5 deletions docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,26 @@ DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres
DATABASE_ENGINE=postgresql


############
# Caching - Configuration for a redis. You can change this to a hosted redis instance.
############

REDIS_HOST=redis
REDIS_PORT=6379

# REDIS_PASSWORD=redis

############
# API - Configuration for the Nginx Reverse proxy.
############

KARRIO_HTTP_PORT=5002


############
# API - Configuration server.
############

## General
JWT_ACCESS_EXPIRY=30 # in minutes
JWT_ACCESS_EXPIRY=30 # in minutes
ALLOW_SIGNUP=false
ALLOW_ADMIN_APPROVED_SIGNUP=false
PERSIST_SDK_TRACING=true
Expand All @@ -51,7 +49,6 @@ EMAIL_HOST=maildev
EMAIL_HOST_USER=[email protected]
EMAIL_HOST_PASSWORD=""


############
# Dashboard - Configuration for the Dashboard
############
Expand Down
3 changes: 2 additions & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ services:
redis:
container_name: karrio.redis
image: redis
restart: unless-stopped
ports:
- ${REDIS_PORT}:6379
restart: unless-stopped
# command: redis-server --requirepass ${REDIS_PASSWORD}
volumes:
- redis-data:/data

Expand Down

0 comments on commit bd3f2d4

Please sign in to comment.