FlaskAPI is fully separates API based on Microservices
For CRUD operations is used Customer
model
Each part is work like Microservice
The Microservice runs in separate Docker container
Microservice has its own Database
Database can be switch from MongoDB to Postgres or other
Also the API include
- authentication mechanism using JWT tokens
- pagination and filtering to limit the amount of data returned
- use caching mechanism to improve performance for frequently accessed data
- rate limiting to prevent abuse of the API
Clone the project
git clone git@github.com:zakharb/flaskapi.git
cd flaskapi
Start docker-compose
docker-compose up -d
Login and get JWT token. Use it in protected requests
curl --location --request POST 'http://localhost:8080/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "admin",
"password": "password"
}'
Add customer
curl --location --request POST 'http://localhost:8080/api/v1/customers' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTY3OTIzNDYyOCwianRpIjoiM2JlNzJjYjktY2QyYi00ZTcyLTg5M2YtOThkZTZhMDVlMTlhIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6ImFkbWluIiwibmJmIjoxNjc5MjM0NjI4LCJleHAiOjE2NzkyMzU1Mjh9.p_rJn8dHb3CIL4zPAgcG6qGsZxJOJZ8O6SL2GlJwGpY' \
--data-raw '{
"name": "John Wick",
"customer_class": "Enduser",
"vat_percentage": 12,
"status": "Active"
}'
Get customer by id
curl -X 'GET' 'http://localhost:8080/api/v1/customers/64170cf4236f4def3a87d600'
Get all customers
curl -X 'GET' 'http://localhost:8080/api/v1/customers/?page_size=3&page=2'
Update customer
curl --location --request PUT 'http://localhost:8080/api/v1/customers/6416c8af76181e5bd5641271' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer paste_token_here_eyJ...U' \
--data-raw '{
"name": "John Doe",
"customer_class": "Enduser",
"vat_percentage": 11,
"status": "Active"
}'
Delete customer
curl --location --request Delete 'http://localhost:8080/api/v1/customers/6416c8af76181e5bd5641271' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer paste_token_here_eyJ...U' \
To solve problem with performance each Service run in container
Gunicorn work as WSGI server and connect to one piece with Nginx
Main configuration is docker-compose.yml
- every service located in separate directory
name-service
- use
Dockerfile
to change docker installation settings - folder
app
contain FastAPI application - all services connected to one piece in
docker-compose.yml
- example of service + DB containers (change
--workers XX
to increase multiprocessing)
Customer
service
customer_service:
build: ./customer-service
command: gunicorn app.main:app --bind 0.0.0.0:8000 --reload
volumes:
- ./customer-service/:/app/
ports:
- 8001:8000
environment:
- DATABASE_URI=mongodb://root:root@mongo:27017/
depends_on:
- customer_db
customer_db:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
logging:
driver: none
Edit Dockerfile
for each Microservice and deploy container
Using SemVer for versioning. For the versions available, see the tags on this repository.
- Zakhar Bengart - Initial work - Ze
See also the list of contributors who participated in this project.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation - see the LICENSE file for details