Use Uvicorn for production with Nginx

master
Mark Veidemanis 2 years ago
parent 966fa37fe8
commit ce551bde75
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -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 Djangos 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 Djangos onload function since browser wont
window.onload();
}
});
}
}

@ -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)

@ -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

@ -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

@ -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;
}
}

@ -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
# 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

@ -5,7 +5,9 @@ django-crispy-forms
crispy-bulma
stripe
django-rest-framework
uwsgi
uvloop
uvicorn[standard]
gunicorn
django-htmx
cryptography
django-debug-toolbar

Loading…
Cancel
Save