From fd46fea548924e8172652a3012c03cf49b372dd5 Mon Sep 17 00:00:00 2001 From: MaelREBOUX Date: Tue, 30 Mar 2021 00:15:25 +0200 Subject: [PATCH] API : first shot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fonctionnel mais il faut que la commande uwsgi -s qui crée le socket soit active dans le shell. --- api/README.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ api/hello.ini | 11 ++++++++ api/hello.py | 16 ++++++++++++ api/wsgi.py | 8 ++++++ nginx/conf_nginx | 7 ++++++ 5 files changed, 107 insertions(+) create mode 100644 api/README.md create mode 100644 api/hello.ini create mode 100644 api/hello.py create mode 100644 api/wsgi.py diff --git a/api/README.md b/api/README.md new file mode 100644 index 0000000..83449d2 --- /dev/null +++ b/api/README.md @@ -0,0 +1,65 @@ + +# API Ar Redadeg + + +## À propos + +Cette API sert à… + + +## Installation + +### Hello.py + + cd api + python3 -m venv venv + . venv/bin/activate + pip install flask uwsgi + +tester + python3 hello.py + +Vérifier sur http://localhost:5000/ + +ctrl + C pour arrêter + +### Configurer uWSGI + +On utilise les fichiers wsgi.py et hello.ini + +on teste si uWSGI et le socket fonctionne : + + uwsgi -s /tmp/hello.sock --manage-script-name --mount /hello=wsgi:app + + +### Configurer nginx + +On rajoute les directives suivantes au fichier de conf nginx + + location = /hello { rewrite ^ /hello/; } + location /hello { try_files $uri @hello; } + location @hello { + include uwsgi_params; + uwsgi_pass unix:/tmp/hello.sock; + } + +Recharger la configuration nginx. + +Tester http://ar-redadeg.openstreetmap.bzh/hello/ + +Super ! Mais il faut que la commande uwsgi -s qui crée le socket soit active dans le shell. + + +### sortir + +deactivate + + + +## Sources + +https://flask.palletsprojects.com/en/1.1.x/deploying/uwsgi/ + +https://www.vultr.com/docs/deploy-a-flask-website-on-nginx-with-uwsgi + +https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04 \ No newline at end of file diff --git a/api/hello.ini b/api/hello.ini new file mode 100644 index 0000000..0bfb9f5 --- /dev/null +++ b/api/hello.ini @@ -0,0 +1,11 @@ +[uwsgi] +module = wsgi:app + +master = true +processes = 5 + +socket = hello.sock +chmod-socket = 660 +vacuum = true + +die-on-term = true diff --git a/api/hello.py b/api/hello.py new file mode 100644 index 0000000..0b48a4f --- /dev/null +++ b/api/hello.py @@ -0,0 +1,16 @@ +#!usr/local/bin/python +# -*- coding: utf-8 -*- + +from flask import Flask + + +app = Flask(__name__) + + +@app.route("/") +def hello(): + return "

Hello There!

" + +if __name__ == "__main__": + app.debug = True + app.run(host='0.0.0.0') diff --git a/api/wsgi.py b/api/wsgi.py new file mode 100644 index 0000000..84f08f3 --- /dev/null +++ b/api/wsgi.py @@ -0,0 +1,8 @@ +#!usr/local/bin/python +# -*- coding: utf-8 -*- + + +from hello import app + +if __name__ == "__main__": + app.run() diff --git a/nginx/conf_nginx b/nginx/conf_nginx index f82ee93..bc5ef10 100644 --- a/nginx/conf_nginx +++ b/nginx/conf_nginx @@ -37,4 +37,11 @@ server { alias /data/projets/mviewer/; } + location = /hello { rewrite ^ /hello/; } + location /hello { try_files $uri @hello; } + location @hello { + include uwsgi_params; + uwsgi_pass unix:/tmp/hello.sock; + } + }