Refactor to use environment variables instead of hardcoded values

- Replace hardcoded values in dokploy.json with environment variable substitution
- Make production settings more environment-aware with better defaults
- Add debug logging to help troubleshoot configuration issues
- Update .env.example with Dokploy-specific configuration
- Add deploy-dokploy.sh helper script for easy environment setup
- Improve ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS handling with fallbacks

This makes the deployment more flexible and follows 12-factor app principles.
This commit is contained in:
thecyberlearn 2025-09-05 10:33:31 +05:30
parent 2bd49184a9
commit f048025ff0
4 changed files with 89 additions and 11 deletions

View File

@ -1,8 +1,13 @@
# Django # Django Configuration
SECRET_KEY=your-secret-key-here-generate-50-random-characters SECRET_KEY=your-secret-key-here-generate-50-random-characters
DEBUG=True DEBUG=false
ALLOWED_HOSTS=localhost,127.0.0.1,your-domain.com DOMAIN=your-domain.com
CSRF_TRUSTED_ORIGINS=http://localhost:8000,http://127.0.0.1:8000 ALLOWED_HOSTS=your-domain.com,*
CSRF_TRUSTED_ORIGINS=https://your-domain.com,http://your-domain.com
# Security Settings (for Dokploy)
SECURE_SSL_REDIRECT=false
SECURE_PROXY_SSL_HEADER=HTTP_X_FORWARDED_PROTO,https
# Email Verification # Email Verification
# Set to False to bypass email verification for testing (until final domain is ready) # Set to False to bypass email verification for testing (until final domain is ready)

54
deploy-dokploy.sh Executable file
View File

@ -0,0 +1,54 @@
#!/bin/bash
# Deployment helper script for Dokploy
# This script helps configure environment variables before deployment
set -e
echo "🚀 Dokploy Deployment Configuration Helper"
echo "=========================================="
# Get domain from user
read -p "Enter your domain (e.g., quamtumtaskai.netcoptech.com): " DOMAIN
if [ -z "$DOMAIN" ]; then
echo "❌ Domain is required"
exit 1
fi
# Generate secret key if not provided
if [ -z "$SECRET_KEY" ]; then
echo "🔑 Generating SECRET_KEY..."
SECRET_KEY=$(python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())')
fi
# Set default environment variables
export DOMAIN="$DOMAIN"
export SECRET_KEY="$SECRET_KEY"
export DEBUG="false"
export ALLOWED_HOSTS="$DOMAIN,*"
export CSRF_TRUSTED_ORIGINS="https://$DOMAIN,http://$DOMAIN"
export SECURE_SSL_REDIRECT="false"
export SECURE_PROXY_SSL_HEADER="HTTP_X_FORWARDED_PROTO,https"
export DOKPLOY_PROJECT_NAME="quantum-tasks-ai"
export PYTHONUNBUFFERED="1"
echo "✅ Configuration complete:"
echo " - Domain: $DOMAIN"
echo " - SECRET_KEY: ${SECRET_KEY:0:20}..."
echo " - ALLOWED_HOSTS: $ALLOWED_HOSTS"
echo " - SSL Redirect: $SECURE_SSL_REDIRECT"
echo ""
echo "📋 Environment variables to set in Dokploy:"
echo "DOMAIN=$DOMAIN"
echo "SECRET_KEY=$SECRET_KEY"
echo "DEBUG=false"
echo "ALLOWED_HOSTS=$ALLOWED_HOSTS"
echo "CSRF_TRUSTED_ORIGINS=$CSRF_TRUSTED_ORIGINS"
echo "SECURE_SSL_REDIRECT=false"
echo "SECURE_PROXY_SSL_HEADER=HTTP_X_FORWARDED_PROTO,https"
echo "DOKPLOY_PROJECT_NAME=quantum-tasks-ai"
echo "PYTHONUNBUFFERED=1"
echo ""
echo "🔄 After setting these variables in Dokploy, trigger a redeploy."
echo "🌐 Your app should be accessible at: https://$DOMAIN"

View File

@ -13,11 +13,13 @@
"env": { "env": {
"PYTHONUNBUFFERED": "1", "PYTHONUNBUFFERED": "1",
"DEBUG": "false", "DEBUG": "false",
"SECRET_KEY": "production-key-change-this-123456789", "SECRET_KEY": "${SECRET_KEY:-production-key-change-this-123456789}",
"ALLOWED_HOSTS": "quamtumtaskai.netcoptech.com,website-quantumtaskai-wrczik-cc50ac-31-97-62-205.traefik.me,*", "ALLOWED_HOSTS": "${ALLOWED_HOSTS:-*}",
"DOKPLOY_PROJECT_NAME": "quantum-tasks-ai", "DOKPLOY_PROJECT_NAME": "quantum-tasks-ai",
"SECURE_SSL_REDIRECT": "false", "SECURE_SSL_REDIRECT": "${SECURE_SSL_REDIRECT:-false}",
"SECURE_PROXY_SSL_HEADER": "HTTP_X_FORWARDED_PROTO,https", "SECURE_PROXY_SSL_HEADER": "HTTP_X_FORWARDED_PROTO,https",
"CSRF_TRUSTED_ORIGINS": "https://quamtumtaskai.netcoptech.com,http://quamtumtaskai.netcoptech.com,https://website-quantumtaskai-wrczik-cc50ac-31-97-62-205.traefik.me,http://website-quantumtaskai-wrczik-cc50ac-31-97-62-205.traefik.me" "CSRF_TRUSTED_ORIGINS": "${CSRF_TRUSTED_ORIGINS:-}",
"DATABASE_URL": "${DATABASE_URL:-}",
"DOMAIN": "${DOMAIN:-quamtumtaskai.netcoptech.com}"
} }
} }

View File

@ -10,10 +10,16 @@ DEBUG = False
# Parse ALLOWED_HOSTS from environment # Parse ALLOWED_HOSTS from environment
allowed_hosts_str = os.environ.get('ALLOWED_HOSTS', '') allowed_hosts_str = os.environ.get('ALLOWED_HOSTS', '')
if allowed_hosts_str: if allowed_hosts_str and allowed_hosts_str != '*':
ALLOWED_HOSTS = [host.strip() for host in allowed_hosts_str.split(',') if host.strip()] ALLOWED_HOSTS = [host.strip() for host in allowed_hosts_str.split(',') if host.strip()]
elif allowed_hosts_str == '*':
ALLOWED_HOSTS = ['*']
else: else:
ALLOWED_HOSTS = [] # Default fallback
domain = os.environ.get('DOMAIN', 'localhost')
ALLOWED_HOSTS = [domain, 'localhost', '127.0.0.1']
print(f"🌐 ALLOWED_HOSTS configured: {ALLOWED_HOSTS}")
# Database - use PostgreSQL if DATABASE_URL is provided, otherwise SQLite # Database - use PostgreSQL if DATABASE_URL is provided, otherwise SQLite
DATABASE_URL = os.environ.get('DATABASE_URL') DATABASE_URL = os.environ.get('DATABASE_URL')
@ -61,7 +67,18 @@ csrf_origins_str = os.environ.get('CSRF_TRUSTED_ORIGINS', '')
if csrf_origins_str: if csrf_origins_str:
CSRF_TRUSTED_ORIGINS = [origin.strip() for origin in csrf_origins_str.split(',') if origin.strip()] CSRF_TRUSTED_ORIGINS = [origin.strip() for origin in csrf_origins_str.split(',') if origin.strip()]
else: else:
CSRF_TRUSTED_ORIGINS = [] # Default CSRF origins based on domain
domain = os.environ.get('DOMAIN', 'localhost')
CSRF_TRUSTED_ORIGINS = [
f'https://{domain}',
f'http://{domain}',
'https://localhost',
'http://localhost'
]
print(f"🔒 CSRF_TRUSTED_ORIGINS configured: {CSRF_TRUSTED_ORIGINS}")
print(f"🔒 SSL Redirect enabled: {SECURE_SSL_REDIRECT}")
print(f"📊 Database: {'PostgreSQL' if DATABASE_URL else 'SQLite'}")
# Logging # Logging
LOGGING = { LOGGING = {