Skip to content

Commit 07fad41

Browse files
committed
allow private IP for healthcheck and make secret key a config val
1 parent 37a1c9e commit 07fad41

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

samples/django-channels-redis/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ This will start the Django server, Redis, and Postgres and mounts your Django ap
2323
## Configuration
2424
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration). Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
2525

26+
### `SECRET_KEY`
27+
The secret key for your Django application. You can generate a new one by running `python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'`. A default, insecure key is used if this is not set, but you should set it for a production deployment.
28+
2629
### `POSTGRES_PASSWORD`
2730
The password for your Postgres database. You need to set this before deploying for the first time.
2831

samples/django-channels-redis/app/django_defang/settings.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from pathlib import Path
1515
import dj_database_url
1616
import urllib.parse
17+
from socket import gethostname, gethostbyname_ex
18+
import ipaddress
1719

1820

1921
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -24,12 +26,25 @@
2426
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
2527

2628
# SECURITY WARNING: keep the secret key used in production secret!
27-
SECRET_KEY = "django-insecure-c!3+-wtsf@&6+p-z88$fyqr!zrnd@uvi=hi&u00n+ku9&_04rk"
29+
SECRET_KEY = os.getenv("SECRET_KEY", "django-insecure-c!3+-wtsf@&6+p-z88$fyqr!zrnd@uvi=hi&u00n+ku9&_04rk")
2830

2931
# SECURITY WARNING: don't run with debug turned on in production!
3032
DEBUG = os.getenv("DEBUG", False) == "True"
3133

32-
ALLOWED_HOSTS = ['*.prod1.defang.dev'] # Set this to your domain name
34+
35+
def get_private_ips():
36+
try:
37+
hostname = gethostname()
38+
_, _, ips = gethostbyname_ex(hostname)
39+
return [ip for ip in ips if ipaddress.ip_address(ip).is_private]
40+
except:
41+
return [] # or return a default list of IPs
42+
43+
ALLOWED_HOSTS = [
44+
'*.prod1.defang.dev',
45+
] # Add your own domain name
46+
47+
ALLOWED_HOSTS += get_private_ips() # Add private IPs so the health check can pass
3348

3449
if DEBUG:
3550
ALLOWED_HOSTS = ["*"]

samples/django-channels-redis/compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
environment:
1313
REDIS_URL: redis://redis_service:6379
1414
POSTGRES_URL:
15+
SECRET_KEY:
1516
healthcheck:
1617
# wget or curl required for healthchecks on services with a published port
1718
# this gets parsed by Defang and provided to the load balancers as well

0 commit comments

Comments
 (0)