From cdb94dc1e35940561639d5795eef4180b62131a8 Mon Sep 17 00:00:00 2001 From: Leandro Roberto Date: Mon, 18 Feb 2019 21:54:06 -0300 Subject: [PATCH] =?UTF-8?q?add=20comunica=C3=A7=C3=A3o=20websocket=20ao=20?= =?UTF-8?q?nginx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/nginx/sapl.conf | 35 +++++++++++++++++++++++++------- daphne_start.sh | 46 ++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 5 ++++- docs/instalacao31.rst | 6 ++++++ sapl/settings.py | 4 +++- start.sh | 7 ++++--- 6 files changed, 91 insertions(+), 12 deletions(-) create mode 100755 daphne_start.sh diff --git a/config/nginx/sapl.conf b/config/nginx/sapl.conf index ef3dac421..3dcf923b1 100644 --- a/config/nginx/sapl.conf +++ b/config/nginx/sapl.conf @@ -1,37 +1,58 @@ upstream sapl_server { - server unix:/var/interlegis/sapl/run/gunicorn.sock fail_timeout=0; - +} +upstream channels_server { + server unix:/var/interlegis/sapl/run/daphne.sock; } server { - listen 80; server_name sapl.test; - client_max_body_size 4G; location /static/ { - alias /var/interlegis/sapl/collected_static/; + alias /var/interlegis/sapl/collected_static/; } location /media/ { - alias /var/interlegis/sapl/media/; + alias /var/interlegis/sapl/media/; } location / { + try_files $uri @proxy_to_app; + } + + location /ws/ { + try_files $uri @proxyto_ws; + } + + location @proxyto_ws { + proxy_pass http://channels_server; + + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; + } + + location @proxy_to_app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; - if (!-f $request_filename) { proxy_pass http://sapl_server; break; } } + error_page 500 502 503 504 /500.html; location = /500.html { root /var/interlegis/sapl/sapl/static/; diff --git a/daphne_start.sh b/daphne_start.sh new file mode 100755 index 000000000..e5339d450 --- /dev/null +++ b/daphne_start.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# As seen in http://tutos.readthedocs.org/en/latest/source/ndg.html + +SAPL_DIR="/var/interlegis/sapl" + +# Seta um novo diretório foi passado como raiz para o SAPL +# caso esse tenha sido passado como parâmetro +if [ "$1" ] +then + SAPL_DIR="$1" +fi + +NAME="SAPL" # Name of the application (*) +DJANGODIR=/var/interlegis/sapl/ # Django project directory (*) +SOCKFILE=/var/interlegis/sapl/run/daphne.sock # we will communicate using this unix socket (*) +USER=`whoami` # the user to run as (*) +GROUP=`whoami` # the group to run as (*) +NUM_WORKERS=3 # how many worker processes should Gunicorn spawn (*) + # NUM_WORKERS = 2 * CPUS + 1 +TIMEOUT=60 +MAX_REQUESTS=100 # number of requests before restarting worker +DJANGO_SETTINGS_MODULE=sapl.settings # which settings file should Django use (*) +DJANGO_ASGI_MODULE=sapl.asgi # WSGI module name (*) + +echo "Starting $NAME as `whoami` on base dir $SAPL_DIR" + +# parameter can be passed to run without virtualenv +if [[ "$@" != "no-venv" ]]; then + # Activate the virtual environment + cd $DJANGODIR + source /var/interlegis/.virtualenvs/sapl/bin/activate + export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE + export PYTHONPATH=$DJANGODIR:$PYTHONPATH +fi + +# Create the run directory if it doesn't exist +RUNDIR=$(dirname $SOCKFILE) +test -d $RUNDIR || mkdir -p $RUNDIR + +# Start your Django Unicorn +exec daphne \ + -u $SOCKFILE ${DJANGO_ASGI_MODULE}:application \ + --access-log /var/log/sapl/daphne_access.log \ + -v2 + diff --git a/docker-compose.yml b/docker-compose.yml index 599abbbf1..b68488992 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ sapldb: volumes: - sapldb_data:/var/lib/postgresql/data/ ports: - - "5432:5432" + - "5434:5432" saplredis: image: redis:5.0.3-stretch restart: always @@ -28,6 +28,9 @@ sapl: EMAIL_HOST_USER: usuariosmtp EMAIL_SEND_USER: usuariosmtp EMAIL_HOST_PASSWORD: senhasmtp + USE_CHANNEL_LAYERS: 'True' + PORT_CHANNEL_LAYERS: 6379 + HOST_CHANNEL_LAYERS: saplredis TZ: America/Sao_Paulo volumes: - sapl_data:/var/interlegis/sapl/data diff --git a/docs/instalacao31.rst b/docs/instalacao31.rst index 3611bda4a..d61f54d5c 100644 --- a/docs/instalacao31.rst +++ b/docs/instalacao31.rst @@ -150,6 +150,10 @@ Criação da `SECRET_KEY > $FILENAME echo "SOLR_COLLECTION = ""${SOLR_COLLECTION-sapl}" >> $FILENAME echo "SOLR_URL = ""${SOLR_URL-http://localhost:8983}" >> $FILENAME - - + echo "USE_CHANNEL_LAYERS = ""${USE_CHANNEL_LAYERS-True}" >> $FILENAME + echo "PORT_CHANNEL_LAYERS = ""${PORT_CHANNEL_LAYERS-6379}" >> $FILENAME + echo "HOST_CHANNEL_LAYERS = ""${HOST_CHANNEL_LAYERS-'saplredis'}" >> $FILENAME echo "[ENV FILE] done." } @@ -109,5 +110,5 @@ echo "| ███████║██║ ██║██║ ████ echo "| ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝ |" echo "-------------------------------------" -/bin/sh gunicorn_start.sh no-venv & +/bin/sh gunicorn_start.sh no-venv & /bin/sh daphne_start.sh no-venv & /usr/sbin/nginx -g "daemon off;"