diff --git a/.env.example b/.env.example index 0ba8c4b..24af327 100644 --- a/.env.example +++ b/.env.example @@ -1,12 +1,17 @@ -DJANGO_SETTINGS_MODULE=config.settings.development -DJANGO_SECRET_KEY=django-insecure-replace-this-with-a-real-secret-key-in-production -DEBUG=True -ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0 +DJANGO_SETTINGS_MODULE=config.settings.production +DJANGO_SECRET_KEY=uqgb2wi9@1dr8alhhx$rp_tx!%_en$k7w6yjbu7wz-qr3$&3-w +DEBUG=False +ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0,radiuma.com,www.radiuma.com +CSRF_TRUSTED_ORIGINS=localhost,127.0.0.1,0.0.0.0,radiuma.com,www.radiuma.com -POSTGRES_DB=tecvico -POSTGRES_USER=tecvico_user -POSTGRES_PASSWORD=tecvico_password +POSTGRES_DB=radiuma +POSTGRES_USER=radiuma_user +POSTGRES_PASSWORD=eS4_WYJH97gywyoHjP6v POSTGRES_HOST=db POSTGRES_PORT=5432 SECURE_SSL_REDIRECT=False + +DJANGO_SUPERUSER_USERNAME=admin +DJANGO_SUPERUSER_EMAIL=admin@yourdomain.com +DJANGO_SUPERUSER_PASSWORD=bS7_U_uRTmivj7W-lCR5 diff --git a/Dockerfile b/Dockerfile index 64a47de..982baa0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,8 +20,6 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . -RUN python manage.py collectstatic --noinput - COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh diff --git a/apps/core/management/commands/ensure_superuser.py b/apps/core/management/commands/ensure_superuser.py new file mode 100644 index 0000000..a04ec36 --- /dev/null +++ b/apps/core/management/commands/ensure_superuser.py @@ -0,0 +1,32 @@ +import os + +from django.contrib.auth import get_user_model +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = "Create a superuser from environment variables if one does not already exist." + + def handle(self, *args, **options): + User = get_user_model() + + username = os.environ.get("DJANGO_SUPERUSER_USERNAME", "admin") + email = os.environ.get("DJANGO_SUPERUSER_EMAIL", "admin@radiuma.com") + password = os.environ.get("DJANGO_SUPERUSER_PASSWORD") + + if not password: + self.stdout.write( + self.style.WARNING( + "DJANGO_SUPERUSER_PASSWORD is not set — skipping superuser creation." + ) + ) + return + + if User.objects.filter(username=username).exists(): + self.stdout.write( + self.style.SUCCESS(f"Superuser '{username}' already exists — skipping.") + ) + return + + User.objects.create_superuser(username=username, email=email, password=password) + self.stdout.write(self.style.SUCCESS(f"Superuser '{username}' created successfully.")) diff --git a/docker-compose.yml b/docker-compose.yml index be5658c..70c2576 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,11 +27,11 @@ services: DJANGO_SETTINGS_MODULE: config.settings.production POSTGRES_HOST: db volumes: - - media_data:/app/media + - ./media:/app/media + - ./staticfiles:/app/staticfiles depends_on: db: condition: service_healthy volumes: postgres_data: - media_data: diff --git a/entrypoint.sh b/entrypoint.sh index 0e41866..54c1211 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -22,8 +22,14 @@ done echo "PostgreSQL is ready." +echo "Collecting static files..." +python manage.py collectstatic --noinput + echo "Running database migrations..." python manage.py migrate --noinput +echo "Creating superuser if not exists..." +python manage.py ensure_superuser + echo "Starting application..." exec "$@"