- Introduced a comprehensive Secret Management Guide detailing best practices, secret classification, development setup, production management, rotation procedures, and emergency protocols. - Implemented a client-side performance monitoring script to track various metrics including page load performance, paint metrics, layout shifts, and memory usage. - Enhanced search accessibility with keyboard navigation support for search results, ensuring compliance with WCAG standards and improving user experience.
ThrillWiki Configuration System
This document provides an overview of the ThrillWiki configuration system, including how settings are organized, how to configure different environments, and best practices for managing configuration.
Architecture Overview
ThrillWiki uses a modular configuration architecture that separates concerns and makes settings easy to manage across different environments.
Directory Structure
backend/config/
├── django/ # Environment-specific settings
│ ├── base.py # Core Django settings (imports from settings/)
│ ├── local.py # Local development settings
│ ├── production.py # Production settings
│ └── test.py # Test settings
├── settings/ # Modular settings modules
│ ├── __init__.py # Package documentation
│ ├── cache.py # Redis caching configuration
│ ├── database.py # Database and GeoDjango settings
│ ├── email.py # Email backend configuration
│ ├── logging.py # Logging formatters, handlers, loggers
│ ├── rest_framework.py # DRF, JWT, CORS, API docs
│ ├── secrets.py # Secret management utilities
│ ├── security.py # Security headers and authentication
│ ├── storage.py # Static/media files and WhiteNoise
│ ├── third_party.py # Allauth, Celery, Cloudflare, etc.
│ └── validation.py # Environment variable validation
└── celery.py # Celery task queue configuration
How Settings Are Loaded
- base.py defines core Django settings and imports all modular settings
- Environment-specific files (local.py, production.py, test.py) extend base.py
- The active settings module is determined by
DJANGO_SETTINGS_MODULE - Modular settings use python-decouple to read environment variables
Environment Selection
The settings module is selected in this order:
DJANGO_SETTINGS_MODULEenvironment variable (explicit override)- Test command detection (
manage.py test→config.django.test) - Production indicators (cloud provider environment variables)
DEBUG=False→config.django.production- Default →
config.django.local
Configuration Methods
Using Environment Variables
Environment variables are the primary configuration method. Create a .env file from the template:
cp .env.example .env
# Edit .env with your values
python-decouple
All settings modules use python-decouple for consistent environment variable handling:
from decouple import config
# String with default
DEBUG = config("DEBUG", default=True, cast=bool)
# Integer with validation
PORT = config("PORT", default=8000, cast=int)
# List from comma-separated string
ALLOWED_HOSTS = config(
"ALLOWED_HOSTS",
default="localhost",
cast=lambda v: [s.strip() for s in v.split(",")]
)
Environment-Specific Configuration
Local Development (config.django.local)
- Debug mode enabled
- Local memory cache (no Redis required)
- Console email backend
- Development middleware (nplusone, debug toolbar)
- Relaxed security settings
Production (config.django.production)
- Debug mode disabled
- Redis caching required
- Strict security settings (HSTS, secure cookies)
- JSON logging for log aggregation
- Sentry integration (optional)
Test (config.django.test)
- In-memory SpatiaLite database
- In-memory cache
- Fast password hashing
- Logging disabled
- Celery tasks run synchronously
Modular Settings Reference
database.py
DATABASES- Database connection configurationGDAL_LIBRARY_PATH,GEOS_LIBRARY_PATH- GeoDjango librariesCONN_MAX_AGE- Connection poolingDATABASE_OPTIONS- PostgreSQL-specific settings
cache.py
CACHES- Redis cache backends (default, sessions, api)SESSION_*- Session storage settingsCACHE_MIDDLEWARE_*- Cache middleware settings
security.py
SECURE_*- Security header settingsSESSION_COOKIE_*,CSRF_COOKIE_*- Cookie securityAUTH_PASSWORD_VALIDATORS- Password validation rulesPERMISSIONS_POLICY- Browser feature permissions
email.py
EMAIL_BACKEND- Email sending backendFORWARD_EMAIL_*- ForwardEmail configuration- SMTP settings for custom email servers
rest_framework.py
REST_FRAMEWORK- DRF configurationCORS_*- Cross-origin settingsSIMPLE_JWT- JWT token settingsREST_AUTH- dj-rest-auth settingsSPECTACULAR_SETTINGS- OpenAPI documentation
third_party.py
ACCOUNT_*,SOCIALACCOUNT_*- Allauth settingsCLOUDFLARE_IMAGES- Image CDN configurationROADTRIP_*- Road trip service settingsHEALTH_CHECK- Health check thresholds
storage.py
STATIC_*,MEDIA_*- File serving configurationSTORAGES- Django 4.2+ storage backendsWHITENOISE_*- Static file optimizationFILE_UPLOAD_*- Upload security limits
logging.py
LOGGING- Complete logging configuration- Formatters: verbose, json, simple
- Handlers: console, file, error_file, performance
- Loggers for Django, application, and third-party
Secret Management
Validation
Use the management command to validate configuration:
# Full validation
python manage.py validate_settings
# Strict mode (warnings become errors)
python manage.py validate_settings --strict
# JSON output
python manage.py validate_settings --json
# Secrets only
python manage.py validate_settings --secrets-only
Required Secrets
These secrets must be set in all environments:
| Secret | Description |
|---|---|
SECRET_KEY |
Django cryptographic signing key (50+ chars) |
DATABASE_URL |
Database connection URL |
Secret Rotation
For production environments:
- Enable rotation checking:
SECRET_ROTATION_ENABLED=True - Track versions:
SECRET_KEY_VERSION=1 - Monitor expiry warnings in logs
- Rotate secrets before expiry
See Secret Management Guide for detailed procedures.
Troubleshooting
Common Issues
Settings module not found:
ModuleNotFoundError: No module named 'config.django.local'
Ensure you're running commands from the backend/ directory.
Required environment variable missing:
decouple.UndefinedValueError: SECRET_KEY not found
Create a .env file or set the environment variable.
Database connection failed:
django.db.utils.OperationalError: could not connect to server
Check DATABASE_URL format and database server status.
Validation Errors
Run validation to identify configuration issues:
python manage.py validate_settings
Debug Configuration
To debug configuration loading:
# In Django shell
from django.conf import settings
print(settings.DEBUG)
print(settings.DATABASES)
Best Practices
- Never commit secrets - Use
.envfiles or secret management services - Use environment variables - Don't hardcode configuration values
- Validate on startup - Catch configuration errors early
- Separate environments - Use different settings for dev/staging/production
- Document custom settings - Add comments explaining non-obvious configuration
- Use appropriate defaults - Secure defaults for production, convenient for development