From ce551bde758395e9fe42893c4cb4d8ae444c8a75 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 12 Oct 2022 07:22:22 +0100 Subject: [PATCH] Use Uvicorn for production with Nginx --- core/static/django-htmx.js | 22 ++++++++++++ core/views/base.py | 4 ++- docker-compose.yml | 6 ++-- docker/docker-compose.prod.yml | 59 ++++++++++++++++++++++++------- docker/nginx/conf.d/default.conf | 23 ++++++++++++ docker/prod/Dockerfile | 4 ++- docker/prod/requirements.prod.txt | 4 ++- 7 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 core/static/django-htmx.js create mode 100644 docker/nginx/conf.d/default.conf diff --git a/core/static/django-htmx.js b/core/static/django-htmx.js new file mode 100644 index 0000000..aaf8eec --- /dev/null +++ b/core/static/django-htmx.js @@ -0,0 +1,22 @@ +{ + const data = document.currentScript.dataset; + const isDebug = data.debug === "True"; + + if (isDebug) { + document.addEventListener("htmx:beforeOnLoad", function (event) { + const xhr = event.detail.xhr; + if (xhr.status == 500 || xhr.status == 404) { + // Tell htmx to stop processing this response + event.stopPropagation(); + + document.children[0].innerHTML = xhr.response; + + // Run Django’s inline script + // (1, eval) wtf - see https://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript + (1, eval)(document.scripts[0].innerText); + // Need to directly call Django’s onload function since browser won’t + window.onload(); + } + }); + } +} diff --git a/core/views/base.py b/core/views/base.py index c711a82..6de8835 100644 --- a/core/views/base.py +++ b/core/views/base.py @@ -1,4 +1,5 @@ import logging +from asyncio import sleep import stripe from django.conf import settings @@ -21,7 +22,8 @@ logger = logging.getLogger(__name__) class Home(View): template_name = "index.html" - def get(self, request): + async def get(self, request): + # await sleep(1) return render(request, self.template_name) diff --git a/docker-compose.yml b/docker-compose.yml index aceb8ab..6f4d381 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,8 +2,8 @@ version: "2" services: app: - image: xf/envelope:latest - build: ./docker + image: xf/envelope:dev + build: ${PORTAINER_GIT_DIR}/docker volumes: - ${PORTAINER_GIT_DIR}:/code - ${APP_LOCAL_SETTINGS}:/code/app/local_settings.py @@ -21,7 +21,7 @@ services: condition: service_started migration: - image: xf/envelope:latest + image: xf/envelope:dev command: sh -c '. /venv/bin/activate && python manage.py migrate --noinput' volumes: - ${PORTAINER_GIT_DIR}:/code diff --git a/docker/docker-compose.prod.yml b/docker/docker-compose.prod.yml index 5ab4956..1540e86 100644 --- a/docker/docker-compose.prod.yml +++ b/docker/docker-compose.prod.yml @@ -2,44 +2,79 @@ version: "2.2" services: app: - image: xf/envelope:latest - build: ./docker/prod + image: xf/envelope:prod + build: ${PORTAINER_GIT_DIR}/docker/prod volumes: - ${PORTAINER_GIT_DIR}:/code - - ${PORTAINER_GIT_DIR}/docker/prod/uwsgi.ini:/conf/uwsgi.ini + # - ${PORTAINER_GIT_DIR}/docker/prod/uwsgi.ini:/conf/uwsgi.ini - ${APP_LOCAL_SETTINGS}:/code/app/local_settings.py - ${APP_DATABASE_FILE}:/code/db.sqlite3 ports: - - "${APP_PORT}:8000" # uwsgi socket + - "8000:8000" # uwsgi socket env_file: - ../stack.env - # volumes_from: - # - tmp + volumes_from: + - tmp depends_on: # redis: # condition: service_healthy migration: condition: service_started + collectstatic: + condition: service_started migration: - image: xf/envelope:latest + image: xf/envelope:prod build: ./docker/prod command: sh -c '. /venv/bin/activate && python manage.py migrate --noinput' volumes: - ${PORTAINER_GIT_DIR}:/code - ${APP_LOCAL_SETTINGS}:/code/app/local_settings.py - ${APP_DATABASE_FILE}:/code/db.sqlite3 + env_file: + - ../stack.env + + collectstatic: + image: xf/envelope:prod + build: ./docker/prod + command: sh -c '. /venv/bin/activate && python manage.py collectstatic --noinput' + volumes: + - ${PORTAINER_GIT_DIR}:/code + - ${APP_LOCAL_SETTINGS}:/code/app/local_settings.py + - ${APP_DATABASE_FILE}:/code/db.sqlite3 + env_file: + - ../stack.env + + nginx: + image: nginx:latest + ports: + - ${APP_PORT}:9999 + ulimits: + nproc: 65535 + nofile: + soft: 65535 + hard: 65535 + volumes: + - ${PORTAINER_GIT_DIR}:/code + - ${PORTAINER_GIT_DIR}/docker/nginx/conf.d:/etc/nginx/conf.d + volumes_from: + - tmp + depends_on: + app: + condition: service_started + + # volumes_from: # - tmp # depends_on: # redis: # condition: service_healthy - # tmp: - # image: busybox - # command: chmod -R 777 /var/run/redis - # volumes: - # - /var/run/redis + tmp: + image: busybox + command: chmod -R 777 /var/run/socks + volumes: + - /var/run/socks # redis: # image: redis diff --git a/docker/nginx/conf.d/default.conf b/docker/nginx/conf.d/default.conf new file mode 100644 index 0000000..9633c7b --- /dev/null +++ b/docker/nginx/conf.d/default.conf @@ -0,0 +1,23 @@ +upstream django { + #server app:8000; + server unix:///var/run/socks/app.sock; +} + +server { + listen 9999; + + location = /favicon.ico { access_log off; log_not_found off; } + + location /static/ { + root /code/core/; + } + + location / { + include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed + proxy_pass http://django; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + } + +} \ No newline at end of file diff --git a/docker/prod/Dockerfile b/docker/prod/Dockerfile index 6900ba7..ad78995 100644 --- a/docker/prod/Dockerfile +++ b/docker/prod/Dockerfile @@ -18,4 +18,6 @@ WORKDIR /code COPY requirements.prod.txt /code/ RUN python -m venv /venv RUN . /venv/bin/activate && pip install -r requirements.prod.txt -CMD . /venv/bin/activate && uwsgi --ini /conf/uwsgi.ini \ No newline at end of file +# CMD . /venv/bin/activate && uwsgi --ini /conf/uwsgi.ini +CMD . /venv/bin/activate && uvicorn --reload --workers 2 --uds /var/run/socks/app.sock app.asgi:application +# CMD . /venv/bin/activate && gunicorn -b 0.0.0.0:8000 --reload app.asgi:application -k uvicorn.workers.UvicornWorker \ No newline at end of file diff --git a/docker/prod/requirements.prod.txt b/docker/prod/requirements.prod.txt index 8905286..7768346 100644 --- a/docker/prod/requirements.prod.txt +++ b/docker/prod/requirements.prod.txt @@ -5,7 +5,9 @@ django-crispy-forms crispy-bulma stripe django-rest-framework -uwsgi +uvloop +uvicorn[standard] +gunicorn django-htmx cryptography django-debug-toolbar