Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
python:fastapi:start [2024/01/14 08:44] marclebrun [Déploiement] |
— (Version actuelle) | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== FastAPI ====== | ||
- | |||
- | * [[https://en.wikipedia.org/wiki/FastAPI]] | ||
- | * [[https://fastapi.tiangolo.com/]] | ||
- | |||
- | ===== Démarrage d'un projet ===== | ||
- | |||
- | <code bash> | ||
- | python3 -m venv venv | ||
- | . venv/bin/activate | ||
- | </code> | ||
- | |||
- | <code bash> | ||
- | pip install fastapi | ||
- | </code> | ||
- | |||
- | <code bash> | ||
- | pip install "uvicorn[standard]" | ||
- | </code> | ||
- | |||
- | <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> | ||
- | |||
- | <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/https/|About HTTPS]] | ||
- | * [[https://fastapi.tiangolo.com/deployment/manually/|Uvicorn]] | ||
- | |||
- | Installer **Uvicorn** | ||
- | |||
- | <code bash> | ||
- | pip install "uvicorn[standard]" | ||
- | </code> | ||
- | |||
- | Lancer le serveur sur le port **80** : | ||
- | |||
- | <code bash> | ||
- | uvicorn main:app --host 0.0.0.0 --port 80 | ||
- | </code> | ||