Outils pour utilisateurs

Outils du site


python:fastapi:start

Ceci est une ancienne révision du document !


FastAPI

Démarrage d'un projet

Créer et activer un environnement virtuel :

python3 -m venv venv
. venv/bin/activate

Intaller FastAPI :

(venv) pip install fastapi

Installer Uvicorn :

(venv) pip install "uvicorn[standard]"

Script de base :

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}

Lancer le serveur en mode développement (reload) :

uvicorn main:app --reload

Déploiement

Créer et activer un environnement virtuel :

python3 -m venv venv
. venv/bin/activate

Intaller FastAPI :

(venv) pip install fastapi

Installer Uvicorn :

pip install "uvicorn[standard]"

Lancer le serveur sur le port 8000 :

uvicorn main:app --host 0.0.0.0 --port 8000

:!: LIRE CECI :

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 :

python3 -m venv venv
. venv/bin/activate

Intaller FastAPI :

(venv) pip install fastapi

Installer a2wsgi :

(venv) pip install a2wsgi

Fichier principal de l'application :

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)

Configuration d'Apache :

<VirtualHost *:80>
        ServerName  api.domaine.com
        ServerAdmin root@domaine.com
 
        RewriteEngine on
        RewriteCond %{HTTPS} !on
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
python/fastapi/start.1705557684.txt.gz · Dernière modification: 2024/01/18 06:01 par marclebrun