Refactor environment setup and enhance development scripts for ThrillWiki

This commit is contained in:
pacnpal
2025-08-20 11:23:05 -04:00
parent 2304085c32
commit 37a20f83ba
8 changed files with 252 additions and 12 deletions

5
.gitignore vendored
View File

@@ -394,4 +394,7 @@ profiles
# Environment files with potential secrets # Environment files with potential secrets
scripts/systemd/thrillwiki-automation***REMOVED*** scripts/systemd/thrillwiki-automation***REMOVED***
scripts/systemd/thrillwiki-deployment***REMOVED*** scripts/systemd/thrillwiki-deployment***REMOVED***
scripts/systemd/****REMOVED*** scripts/systemd/****REMOVED***
logs/
profiles/
uv.lock

View File

@@ -22,7 +22,7 @@ env = environ.Env(
BASE_DIR = Path(__file__).resolve().parent.parent.parent BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Read environment file if it exists # Read environment file if it exists
environ.Env.read_env(BASE_DIR / '***REMOVED***') environ.Env.read_env(BASE_DIR / '.env')
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY') SECRET_KEY = env('SECRET_KEY')

View File

@@ -94,11 +94,14 @@ for middleware in DEVELOPMENT_MIDDLEWARE:
INTERNAL_IPS = ['127.0.0.1', '::1'] INTERNAL_IPS = ['127.0.0.1', '::1']
# Silk configuration for development # Silk configuration for development
SILKY_PYTHON_PROFILER = True SILKY_PYTHON_PROFILER = False # Disable profiler to avoid silk_profile installation issues
SILKY_PYTHON_PROFILER_BINARY = True SILKY_PYTHON_PROFILER_BINARY = False # Disable binary profiler
SILKY_PYTHON_PROFILER_RESULT_PATH = BASE_DIR / 'profiles' # SILKY_PYTHON_PROFILER_RESULT_PATH = BASE_DIR / 'profiles' # Not needed when profiler is disabled
SILKY_AUTHENTICATION = True SILKY_AUTHENTICATION = True # Require login to access Silk
SILKY_AUTHORISATION = True SILKY_AUTHORISATION = True # Enable authorization
SILKY_MAX_REQUEST_BODY_SIZE = -1 # Don't limit request body size
SILKY_MAX_RESPONSE_BODY_SIZE = 1024 # Limit response body size to 1KB for performance
SILKY_META = True # Record metadata about requests
# NPlusOne configuration # NPlusOne configuration
import logging import logging

View File

@@ -7,11 +7,11 @@ import sys
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
if 'test' in sys.argv and 'accounts' in sys.argv: if 'test' in sys.argv and 'accounts' in sys.argv:
os***REMOVED***iron.setdefault("DJANGO_SETTINGS_MODULE", "config.django.test_accounts") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.django.test_accounts")
elif 'test' in sys.argv: elif 'test' in sys.argv:
os***REMOVED***iron.setdefault("DJANGO_SETTINGS_MODULE", "config.django.test") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.django.test")
else: else:
os***REMOVED***iron.setdefault("DJANGO_SETTINGS_MODULE", "config.django.local") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.django.local")
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:

94
scripts/README.md Normal file
View File

@@ -0,0 +1,94 @@
# ThrillWiki Development Scripts
## Development Server Script
The `dev_server.sh` script sets up all necessary environment variables and starts the Django development server with proper configuration.
### Usage
```bash
# From the project root directory
./scripts/dev_server.sh
# Or from anywhere
/path/to/thrillwiki_django_no_react/scripts/dev_server.sh
```
### What the script does
1. **Environment Setup**: Sets all required environment variables for local development
2. **Directory Creation**: Creates necessary directories (logs, profiles, media, etc.)
3. **Database Migrations**: Runs pending migrations automatically
4. **Superuser Creation**: Creates a development superuser (admin/admin) if none exists
5. **Static Files**: Collects static files for the application
6. **Tailwind CSS**: Builds Tailwind CSS if npm is available
7. **System Checks**: Runs Django system checks
8. **Server Start**: Starts the Django development server on `http://localhost:8000`
### Environment Variables Set
The script automatically sets these environment variables:
- `DJANGO_SETTINGS_MODULE=config.django.local`
- `DEBUG=True`
- `SECRET_KEY=<generated-dev-key>`
- `ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0`
- `DATABASE_URL=postgis://thrillwiki_user:thrillwiki_pass@localhost:5432/thrillwiki_db`
- `CACHE_URL=locmemcache://`
- `CORS_ALLOW_ALL_ORIGINS=True`
- GeoDjango library paths for macOS
- And many more...
### Prerequisites
1. **PostgreSQL with PostGIS**: Make sure PostgreSQL with PostGIS extension is running
2. **Database**: Create the database `thrillwiki_db` with user `thrillwiki_user`
3. **uv**: The script uses `uv` to run Django commands
4. **Virtual Environment**: The script will activate `.venv` if it exists
### Database Setup
If you need to set up the database:
```bash
# Install PostgreSQL and PostGIS (macOS with Homebrew)
brew install postgresql postgis
# Start PostgreSQL
brew services start postgresql
# Create database and user
psql postgres -c "CREATE USER thrillwiki_user WITH PASSWORD 'thrillwiki_pass';"
psql postgres -c "CREATE DATABASE thrillwiki_db OWNER thrillwiki_user;"
psql -d thrillwiki_db -c "CREATE EXTENSION postgis;"
psql -d thrillwiki_db -c "GRANT ALL PRIVILEGES ON DATABASE thrillwiki_db TO thrillwiki_user;"
```
### Access Points
Once the server is running, you can access:
- **Main Application**: http://localhost:8000
- **Admin Interface**: http://localhost:8000/admin/ (admin/admin)
- **Django Silk Profiler**: http://localhost:8000/silk/
- **API Documentation**: http://localhost:8000/api/docs/
- **API Redoc**: http://localhost:8000/api/redoc/
### Stopping the Server
Press `Ctrl+C` to stop the development server.
### Troubleshooting
1. **Database Connection Issues**: Ensure PostgreSQL is running and the database exists
2. **GeoDjango Library Issues**: Adjust `GDAL_LIBRARY_PATH` and `GEOS_LIBRARY_PATH` if needed
3. **Permission Issues**: Make sure the script is executable with `chmod +x scripts/dev_server.sh`
4. **Virtual Environment**: Ensure your virtual environment is set up with all dependencies
### Customization
You can modify the script to:
- Change default database credentials
- Adjust library paths for your system
- Add additional environment variables
- Modify the development server port or host

140
scripts/dev_server.sh Executable file
View File

@@ -0,0 +1,140 @@
#!/bin/bash
# ThrillWiki Development Server Script
# This script sets up the proper environment variables and runs the Django development server
set -e # Exit on any error
echo "🚀 Starting ThrillWiki Development Server..."
# Change to the project directory (parent of scripts folder)
cd "$(dirname "$0")/.."
# Set Django environment to local development
export DJANGO_SETTINGS_MODULE="config.django.local"
# Core Django settings
export DEBUG="True"
export SECRET_KEY="django-insecure-dev-key-not-for-production-$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)"
# Allowed hosts for development
export ALLOWED_HOSTS="localhost,127.0.0.1,0.0.0.0"
# CSRF trusted origins for development
export CSRF_TRUSTED_ORIGINS="http://localhost:8000,http://127.0.0.1:8000,https://127.0.0.1:8000"
# Database configuration (PostgreSQL with PostGIS)
export DATABASE_URL="postgis://thrillwiki_user:thrillwiki_pass@localhost:5432/thrillwiki_db"
# Cache configuration (use locmem for development if Redis not available)
export CACHE_URL="locmemcache://"
export REDIS_URL="redis://127.0.0.1:6379/1"
# CORS settings for API development
export CORS_ALLOW_ALL_ORIGINS="True"
export CORS_ALLOWED_ORIGINS=""
# Email configuration for development (console backend)
export EMAIL_URL="consolemail://"
# GeoDjango library paths for macOS (adjust if needed)
export GDAL_LIBRARY_PATH="/opt/homebrew/lib/libgdal.dylib"
export GEOS_LIBRARY_PATH="/opt/homebrew/lib/libgeos_c.dylib"
# API rate limiting (generous for development)
export API_RATE_LIMIT_PER_MINUTE="1000"
export API_RATE_LIMIT_PER_HOUR="10000"
# Cache settings
export CACHE_MIDDLEWARE_SECONDS="1" # Very short cache for development
export CACHE_MIDDLEWARE_KEY_PREFIX="thrillwiki_dev"
# Social auth settings (you can set these if you have them)
# export GOOGLE_OAUTH2_CLIENT_ID=""
# export GOOGLE_OAUTH2_CLIENT_SECRET=""
# export DISCORD_CLIENT_ID=""
# export DISCORD_CLIENT_SECRET=""
# Create necessary directories
echo "📁 Creating necessary directories..."
mkdir -p logs
mkdir -p profiles
mkdir -p media
mkdir -p staticfiles
mkdir -p static/css
# Check if virtual environment is activated
if [[ -z "$VIRTUAL_ENV" ]] && [[ -d ".venv" ]]; then
echo "🔧 Activating virtual environment..."
source .venv/bin/activate
fi
# Run database migrations if needed
echo "🗄️ Checking database migrations..."
if uv run manage.py migrate --check 2>/dev/null; then
echo "✅ Database migrations are up to date"
else
echo "🔄 Running database migrations..."
uv run manage.py migrate --noinput
fi
# Create superuser if it doesn't exist
echo "👤 Checking for superuser..."
if ! uv run manage.py shell -c "from django.contrib.auth import get_user_model; User = get_user_model(); exit(0 if User.objects.filter(is_superuser=True).exists() else 1)" 2>/dev/null; then
echo "👤 Creating development superuser (admin/admin)..."
uv run manage.py shell -c "
from django.contrib.auth import get_user_model
User = get_user_model()
if not User.objects.filter(username='admin').exists():
User.objects.create_superuser('admin', 'admin@example.com', 'admin')
print('Created superuser: admin/admin')
else:
print('Superuser already exists')
"
fi
# Collect static files for development
echo "📦 Collecting static files..."
uv run manage.py collectstatic --noinput --clear
# Build Tailwind CSS
if command -v npm &> /dev/null; then
echo "🎨 Building Tailwind CSS..."
uv run manage.py tailwindcss --build
else
echo "⚠️ npm not found, skipping Tailwind CSS build"
fi
# Run system checks
echo "🔍 Running system checks..."
if uv run manage.py check; then
echo "✅ System checks passed"
else
echo "❌ System checks failed, but continuing..."
fi
# Display environment info
echo ""
echo "🌍 Development Environment:"
echo " - Settings Module: $DJANGO_SETTINGS_MODULE"
echo " - Debug Mode: $DEBUG"
echo " - Database: PostgreSQL with PostGIS"
echo " - Cache: Local memory cache"
echo " - Admin URL: http://localhost:8000/admin/"
echo " - Admin User: admin / admin"
echo " - Silk Profiler: http://localhost:8000/silk/"
echo " - Debug Toolbar: Available on debug pages"
echo " - API Documentation: http://localhost:8000/api/docs/"
echo ""
# Start the development server
echo "🌟 Starting Django development server on http://localhost:8000"
echo "Press Ctrl+C to stop the server"
echo ""
# Use runserver_plus if django-extensions is available, otherwise use standard runserver
if uv run python -c "import django_extensions" 2>/dev/null; then
exec uv run manage.py runserver_plus 0.0.0.0:8000
else
exec uv run manage.py runserver 0.0.0.0:8000
fi

View File

@@ -95,7 +95,7 @@ urlpatterns = [
path("moderation/", include("moderation.urls", namespace="moderation")), path("moderation/", include("moderation.urls", namespace="moderation")),
path( path(
"env-settings/", "env-settings/",
views***REMOVED***ironment_and_settings_view, views.environment_and_settings_view,
name="environment_and_settings", name="environment_and_settings",
), ),
] ]

View File

@@ -119,7 +119,7 @@ class SearchView(TemplateView):
def environment_and_settings_view(request): def environment_and_settings_view(request):
# Get all environment variables # Get all environment variables
env_vars = dict(os***REMOVED***iron) env_vars = dict(os.environ)
# Get all Django settings as a dictionary # Get all Django settings as a dictionary
settings_vars = {setting: getattr(settings, setting) for setting in dir(settings) if setting.isupper()} settings_vars = {setting: getattr(settings, setting) for setting in dir(settings) if setting.isupper()}