Skip to content

config_serveur

Steven Jeanneret edited this page Mar 14, 2019 · 2 revisions

Déploiement API REST et front end dans la HE-Arc

Dans cette configuration, il s'agit d'une API REST avec Django REST framework et d'un front end NUXTJS.

Pour faire fonctionner une API back end et un front end sur le même port et le même sous domaine, il faut que toutes les routes du back end commencent par /api/ (ou un autre nom distinctif).

Configuration des routes dans le projet Django

Toutes les routes des applications importées comme ci-dessous seront automatiquement préfixées par /api/.

urlpatterns = [
  path('api/', include('core.urls')),
  path('api/', include('auth0.urls')),
]

Configuration Nginx

Dans la configuration Nginx ci-dessous, on redirige les uris commençant /api/ vers notre api Django.

Pour NUXTJS, contrairement à la plus part des front end, il s'agit d'un serveur qui écoute sur le port 3000 par défaut. Dans le cas où une uri ne contient pas /api on va donc la rediriger sur le port 3000 local et le service NUXTJS s'occupera du reste.

Pour un front end généré statiquement, il suffirait de changer root avec le chemin vers le fichier index.html et de supprimer la section location / {...

Source

# Nginx configuration
# -------------------
# vim: set ft=nginx:
#

map $http_upgrade $connection_upgrade {
    default Upgrade;
    ''      close;
}

upstream uwsgi {
    server unix:/tmp/uwsgi.sock fail_timeout=0;
}


server {
    listen 80;
    server_name _;

    client_max_body_size 4G;

    root /var/www/app/public;

    access_log /var/www/logs/access.log;
    error_log /var/www/logs/error.log;

    index index.html index.htm;

    # WebSocket
    location /ws {
        proxy_pass http://uwsgi;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        # WebSocket proxy.
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
    }

    location / {
	proxy_pass http://localhost:3000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
    }

    location ~ ^/api {
        try_files $uri @wsgi;
    }

    location @wsgi {
        uwsgi_pass uwsgi;
        include uwsgi_params;
    }
}

Configuration UWSGI

Ci-dessous, la configuration de UWSGI, ou chdir doit pointer à la racine du projet Django, la ou est le fichier manage.py.

[uwsgi]
plugin=python3

; Socket must be writable by nginx.
socket=/tmp/uwsgi.sock
chmod-socket=777
uid=www-data
gid=www-data

; Environment variables from the container.
envdir=/etc/container_environment
; Application should exist within a virtualenv.
chdir=/var/www/ListView/current/backend/
virtualenv=/var/www/ListView/shared/env
module=backend.wsgi:application

; Logging
logto=/var/www/logs/uwsgi.log

Capistrano

La configuration du fichier production.rb pour déployer le front end également.

Pour une configuration complète voir ici.

server "listview.srvz-webapp.he-arc.ch", user: "poweruser", roles: %w{app db web}, port:2272

set :application, 'ListView'
set :deploy_to, "/var/www/#{fetch(:application)}"
set :repo_url, "git@github.com:HE-Arc/ListView.git"

after "deploy:published", "restart_sidekiq"
after "deploy:publishing", "uwsgi:restart"

after 'deploy:updating', 'python:create_venv'
after 'deploy:updating', 'nuxtjs:download'

after 'deploy:published', 'nuxtjs:restart'

# Task
task :restart_sidekiq do
  on roles(:worker) do
    execute :service, "sidekiq restart"
  end
end

namespace :uwsgi do
    desc "Restart application"
    task :restart do
        on roles(:web) do |h|
	    execute :sudo, "sv reload uwsgi"
	end
    end
end

namespace :python do

    def venv_path
        File.join(shared_path, "env")
    end

    desc "Create venv"
    task :create_venv do
        on roles([:app, :web]) do |h|
	    execute "python3.6 -m venv #{venv_path}"
        execute "source #{venv_path}/bin/activate"
	    execute "#{venv_path}/bin/pip install -r #{release_path}/backend/requirements.txt"
        end
    end
end

namespace :nuxtjs do
	desc "Install dependencies and reload nuxtjs"
	task :download do
		on roles(:web) do |h|
			execute "cd #{release_path}/ListView_frontend/ && npm install"
			execute "cd #{release_path}/ListView_frontend/ && nuxt build"
		end
	end
	task :restart do
		on roles(:web) do |h|
			execute :sudo, "sv restart nuxtjs"
		end
	end
end