From c35f1e6326289ea7059eb923db1bd4159fba7130 Mon Sep 17 00:00:00 2001 From: Antti Viitala Date: Mon, 18 Dec 2023 19:22:45 +0800 Subject: [PATCH] chore: polish readme, update settings --- .github/workflows/build-container.yaml | 2 +- Dockerfile | 4 +++ README.md | 36 ++++++++++++++++++-------- deployment/server-entrypoint.sh | 3 +++ railway_django_stack/settings.py | 6 ++++- requirements.txt | 3 ++- static/.gitkeep | 0 7 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 static/.gitkeep diff --git a/.github/workflows/build-container.yaml b/.github/workflows/build-container.yaml index 2c94aec..34244bb 100644 --- a/.github/workflows/build-container.yaml +++ b/.github/workflows/build-container.yaml @@ -39,6 +39,6 @@ jobs: context: ./. file: ./Dockerfile push: true - tags: ${{env.REGISTRY}}:${{github.run_number}} + tags: ${{env.REGISTRY}}:${{github.run_number}}, ${{env.REGISTRY}}:latest cache-from: type=gha cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile index 69b8ff1..b660cba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,6 +37,10 @@ RUN apt-get -y purge gcc libc-dev python3-dev # Add all application code from this folder, including deployment entrypoints COPY --chown=python:python ./ /app +# Create staticfiles folder +RUN mkdir -p staticfiles && \ + chown -R python:python staticfiles + # Make entrypoints executable RUN chmod +x /app/deployment/server-entrypoint.sh && \ chmod +x /app/deployment/worker-entrypoint.sh diff --git a/README.md b/README.md index c9a0b98..4b28742 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,29 @@ # `railway_django_stack` - +[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/NBR_V3?referralCode=6rOei9) + +Full Django/Postgres stack with Celery tasks and Redis as cache/queue. + +## Overview + +WIP + +## Deploying the template + +1. In the deployment screen, you will need to configure a `DJANGO_SECRET_KEY`. You can use the below snippet to do that or otherwise generate your own. +
+ Snippet to create secret + + **This assumes your default python installation has Django installed.** + + ```bash + python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())' + ``` + +
+1. Once the containers have been deployed, please take the following steps to delete public proxy addresses, as you will not need to access the private services directly: + - Go to the **Postgres** service > Settings > Networking, delete the proxy + - Go to the **Redis** service > Settings > Networking, delete the proxy ## Resources @@ -55,14 +78,5 @@ This is a barebones Django-project with the following additions/updates: - [`django-celery-beat`](https://github.com/celery/django-celery-beat) for periodic task management - [`django-celery-results`](https://github.com/celery/django-celery-results) for viewing results of Celery tasks in Django Admin - Uses [`python-decouple`](https://github.com/HBNetwork/python-decouple) to manage settings via environment varialbes +- Uses [`whitenoise`](https://github.com/evansd/whitenoise) to make serving static assets easy - Installs and runs with [`gunicorn`](https://github.com/benoitc/gunicorn) - -## Variable configurations - -| Variable | Description | Used by | -| -------- | ----------- | ------- | -`POSTGRES_HOST` | This should be the private URL of the Postgres service | Django, Celery -`POSTGRES_DB` | Sets the Postgres default database name | Django, Celery, Postgres -`POSTGRES_USER` | Sets the Postgres username | Django, Celery, Postgres -`POSTGRES_PASSWORD` | Sets the Postgres user's password | Django, Celery, Postgres -`REDIS_URL` | Configures the private URl of the Redis service | Django, Celery | diff --git a/deployment/server-entrypoint.sh b/deployment/server-entrypoint.sh index e810668..8125fdf 100755 --- a/deployment/server-entrypoint.sh +++ b/deployment/server-entrypoint.sh @@ -1,5 +1,8 @@ #!/bin/sh +echo "Collecting static files.." +python manage.py collectstatic --noinput + if [ "$RUN_MIGRATIONS" = "True" ]; then echo "Running migrations..." until python manage.py migrate diff --git a/railway_django_stack/settings.py b/railway_django_stack/settings.py index c0d6b1d..aa15bb6 100644 --- a/railway_django_stack/settings.py +++ b/railway_django_stack/settings.py @@ -11,6 +11,7 @@ """ from pathlib import Path +import os from decouple import config @@ -24,7 +25,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config("DEBUG", default=False, cast=bool) -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ["*"] # Application definition @@ -49,6 +50,7 @@ "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", + "whitenoise.middleware.WhiteNoiseMiddleware", ] ROOT_URLCONF = "railway_django_stack.urls" @@ -136,6 +138,8 @@ # https://docs.djangoproject.com/en/4.2/howto/static-files/ STATIC_URL = "static/" +STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] +STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/requirements.txt b/requirements.txt index d3fbd61..a6ab41e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ django-celery-results==2.5.1 gunicorn==21.2.0 psycopg[binary]==3.1.15 python-decouple==3.8 -redis==5.0.1 \ No newline at end of file +redis==5.0.1 +whitenoise==6.6.0 \ No newline at end of file diff --git a/static/.gitkeep b/static/.gitkeep new file mode 100644 index 0000000..e69de29