Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente | |||
python:fastapi:start [2024/03/20 08:43] marclebrun supprimée |
— (Version actuelle) | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== FastAPI ====== | ||
- | |||
- | * [[https://en.wikipedia.org/wiki/FastAPI]] | ||
- | * [[https://fastapi.tiangolo.com/]] | ||
- | |||
- | ===== Démarrage d'un projet ===== | ||
- | |||
- | * [[https://fastapi.tiangolo.com/tutorial/]] | ||
- | |||
- | Créer et activer un environnement virtuel : | ||
- | |||
- | <code bash> | ||
- | python3 -m venv venv | ||
- | . venv/bin/activate | ||
- | </code> | ||
- | |||
- | Intaller FastAPI : | ||
- | |||
- | <code bash> | ||
- | (venv) pip install fastapi | ||
- | </code> | ||
- | |||
- | Installer Uvicorn : | ||
- | |||
- | <code bash> | ||
- | (venv) pip install "uvicorn[standard]" | ||
- | </code> | ||
- | |||
- | Script de base : | ||
- | |||
- | <code python> | ||
- | from fastapi import FastAPI | ||
- | |||
- | app = FastAPI() | ||
- | |||
- | @app.get("/") | ||
- | def index(): | ||
- | return {"message": "Hello there!"} | ||
- | |||
- | app.get("/hello/{name}") | ||
- | def hello(name:str): | ||
- | return {"message": "Hello %s !" % name} | ||
- | </code> | ||
- | |||
- | Lancer le serveur en mode développement (reload) : | ||
- | |||
- | <code bash> | ||
- | uvicorn main:app --reload | ||
- | </code> | ||
- | |||
- | * Requête **index** : [[http://localhost:8000]] | ||
- | * Requête **hello** : [[http://localhost:8000/hello/Marc]] | ||
- | * Interface **Swagger UI** : [[http://localhost:8000/docs]] | ||
- | |||
- | ===== Déploiement ===== | ||
- | |||
- | * [[https://fastapi.tiangolo.com/deployment/]] | ||
- | |||
- | Créer et activer un environnement virtuel : | ||
- | |||
- | <code bash> | ||
- | python3 -m venv venv | ||
- | . venv/bin/activate | ||
- | </code> | ||
- | |||
- | Intaller FastAPI : | ||
- | |||
- | <code bash> | ||
- | (venv) pip install fastapi | ||
- | </code> | ||
- | |||
- | Installer Uvicorn : | ||
- | |||
- | <code bash> | ||
- | pip install "uvicorn[standard]" | ||
- | </code> | ||
- | |||
- | Lancer le serveur sur le port **8000** : | ||
- | |||
- | <code bash> | ||
- | uvicorn main:app --host 0.0.0.0 --port 8000 | ||
- | </code> | ||
- | |||
- | :!: **LIRE CECI :** | ||
- | * [[https://docs.masternetwork.dev/host-an-api-with-ssl/]] | ||
- | * [[https://community.letsencrypt.org/t/certbot-not-working-with-uvicorn-fastapi-on-ubuntu/187849]] | ||
- | * [[https://github.com/fnep/example_fastapi_behind_apache_using_wsgi/blob/main/apache-vhost.conf]] | ||
- | |||
- | ===== Déploiement avec WSGI sous Apache ===== | ||
- | |||
- | Source: [[https://github.com/fnep/example_fastapi_behind_apache_using_wsgi/tree/main]] | ||
- | |||
- | Créer et activer un environnement virtuel : | ||
- | |||
- | <code bash> | ||
- | python3 -m venv venv | ||
- | . venv/bin/activate | ||
- | </code> | ||
- | |||
- | Intaller **FastAPI** : | ||
- | |||
- | <code bash> | ||
- | (venv) pip install fastapi | ||
- | </code> | ||
- | |||
- | Installer **a2wsgi** : | ||
- | |||
- | <code bash> | ||
- | (venv) pip install a2wsgi | ||
- | </code> | ||
- | |||
- | Fichier de lancement de l'application **wsgi.py** : | ||
- | |||
- | <code python wsgi.py> | ||
- | from fastapi import FastAPI | ||
- | from a2wsgi import ASGIMiddleware | ||
- | |||
- | app = FastAPI() | ||
- | |||
- | @app.get("/") | ||
- | def index(): | ||
- | return { | ||
- | "messsage": "Hello world !" | ||
- | } | ||
- | |||
- | @app.get("/hello/{name}") | ||
- | def hello(name:str): | ||
- | return { | ||
- | "message": "Hello %s !" % name | ||
- | } | ||
- | |||
- | # mod_wsgi expects the name 'application' by default | ||
- | application = ASGIMiddleware(app) | ||
- | </code> | ||
- | |||
- | Configuration d'Apache : | ||
- | |||
- | <code apache> | ||
- | <VirtualHost *:80> | ||
- | ServerName api.domaine.com | ||
- | ServerAdmin root@domaine.com | ||
- | |||
- | RewriteEngine on | ||
- | RewriteCond %{HTTPS} !on | ||
- | RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} | ||
- | </VirtualHost> | ||
- | |||
- | <VirtualHost *:443> | ||
- | ServerName api.domaine.com | ||
- | ServerAdmin root@domaine.com | ||
- | |||
- | WSGIDaemonProcess fastapi_app user=www-data group=www-data python-home=/var/www/api_domaine_com/venv threads=5 | ||
- | WSGIProcessGroup fastapi_app | ||
- | WSGIScriptAlias / /var/www/api_domaine_com/wsgi.py | ||
- | |||
- | DocumentRoot /var/www/api_domaine_com | ||
- | <Directory /var/www/api_domaine_com> | ||
- | Options Indexes FollowSymLinks MultiViews | ||
- | AllowOverride all | ||
- | Require all denied | ||
- | Require all granted | ||
- | allow from all | ||
- | </Directory> | ||
- | |||
- | SSLEngine on | ||
- | SSLCertificateFile /etc/letsencrypt/live/api.domaine.com/cert.pem | ||
- | SSLCertificateKeyFile /etc/letsencrypt/live/api.domaine.com/privkey.pem | ||
- | SSLCertificateChainFile /etc/letsencrypt/live/api.domaine.com/chain.pem | ||
- | SSLProtocol all -SSLv2 -SSLv3 | ||
- | SSLHonorCipherOrder on | ||
- | SSLCompression off | ||
- | SSLOptions +StrictRequire | ||
- | SSLCipherSuite ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA | ||
- | Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" | ||
- | |||
- | ErrorLog ${APACHE_LOG_DIR}/api.domaine.com/error.log | ||
- | CustomLog ${APACHE_LOG_DIR}/api.domaine.com/access.log combined | ||
- | </VirtualHost> | ||
- | </code> | ||