This is a FastAPI training for beginners. This training includes building a simple web API using FastAPI library. There are three files with the app and three files with response models. The highest version, the more advanced techniques are used. File main.py is compatible with response_models_v1, Files main_v1.py, main_v2.py are compatible with response_models_v1, response_models_v2, response_models_v3.
More info: FastAPI
Currently, the repository consists of folders:
- api - contains the FastAPI
- power point presentation
In order to run this module it is recommended to have installed the following software versions:
- Python 3.9 (preferably 3.9.13)
- Package Manager (e.g. virtualenv)
- Docker
-
Clone project repository or download zip package from Wiki page
-
Create virtual environment in the project folder:
python -m venv venv_name
- Activate virtual environment:
venv_name\Scripts\activate
- Install all necessary python packages:
pip install -r requirements.txt
- Run web api:
uvicorn main_v1:app --reload --port 1234 --host 127.0.0.1
Flags:
- --reload - Enable auto-reload.
- --port - Bind socket to this port. [default: 8000]
- --host - Bind socket to this host. [default: 127.0.0.1]
Automatic interactive API documentation (provided by Swagger UI) can be accessed on:
This web api also accept request for client REST API such as Insomia, PostMan
Configure
- Install and configure docker in WSL: gitlab instruction
- open linux terminal in project folder
- build docker image from Dockerfile
sudo docker build -t image_name .
- run docker image
sudo docker run --name container_name -p 5000:5005 image_name
where
- 5005 is port exposed in dockerfile,
- 5000 is the port, where web api is available n the host machine
- Navigate to http://127.0.0.1:5000/docs#/
Check image status:
sudo docker images
Check container status:
sudo docker ps -a