zakharb / MicroAPI

Micro Services with FastAPI and Docker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo

description

usage

🟩 Getting Started

MicroAPI is fully separates API in Async mode based on Microservices

For CRUD operations is used Customer - Product - Price - Order model

The Customer buys a Product with a different Price and receives an Order
The Price is calculated including taxes/discounts depending on the type of customer/

Each part is work like Microservice
The Microservice runs in separate Docker container

Microservice has its own Database
Database can be switch from Postgres to MongoDB or other

Requirements

FastAPI Docker Postgres Nginx

Installing

Clone the project

git clone git@github.com:zakharb/microapi.git
cd microapi

Start docker-compose

docker-compose up -d

animated

🟩 Usage

Customers

Get, put, update, delete Customers via API Customers

animated

Products

Get, put, update, delete Products via API Products

Prices

Get Prices via API Prices

Orders

Get Orders via API Orders

🟩 Configuration

To solve problem with performance each Service run in container
Uvicorn work as ASGI 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)

Examples

Customer service

  customer_service:
    build: ./customer-service
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4
    #command: gunicorn main:app --workers 4 --worker-class --host 0.0.0.0 --port 8000
    volumes:
      - ./customer-service/:/app/
    ports:
      - 8001:8000
    environment:
      - DATABASE_URI=postgresql://customer_db_username:customer_db_password@customer_db/customer_db_dev
    depends_on:
       - customer_db
    logging:
        driver: none 
  
  customer_db:
    image: postgres:latest
    volumes:
      - postgres_data_customer:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=customer_db_username
      - POSTGRES_PASSWORD=customer_db_password
      - POSTGRES_DB=customer_db_dev
    logging:
        driver: none 

🟩 Client

There is client for work with Microservices
Microapiclient is used for generating data and testing

It is located in folder client

Install

cd client
python3 -m venv venv
source venv/bin/activate 
python -m pip install -e .

Run

cd client
source venv/bin/activate 
python -m microapiclient

  __   __   _                      _____   _____  _______ 
 (__)_(__) (_)        _           (_____) (_____)(_______)
(_) (_) (_) _    ___ (_)__  ___  (_)___(_)(_)__(_)  (_)   
(_) (_) (_)(_) _(___)(____)(___) (_______)(_____)   (_)   
(_)     (_)(_)(_)___ (_)  (_)_(_)(_)   (_)(_)     __(_)__ 
(_)     (_)(_) (____)(_)   (___) (_)   (_)(_)    (_______)

usage: __main__.py [-h]
                   {getcustomer,postcustomer,getproduct,postproduct,getprice,postorder,postorders,generateorder}
                   ...

positional arguments:
  {getcustomer,postcustomer,getproduct,postproduct,getprice,postorder,postorders,generateorder}
    getcustomer         Get customer from DB
    postcustomer        Post new customer into DB
    getproduct          Get product by name from DB
    postproduct         Post new product into DB
    getprice            Get price_net and price_gross
    postorder           Post order into DB
    postorders          Bulk write orders into DB
    generateorder       Generate order into CSV file

optional arguments:
  -h, --help            show this help message and exit

Examples

  • Generate Customers
python -m microapiclient postcustomer --customer-count 50
  • Generate Products
python -m microapiclient postproduct --product-count 50
  • Generate Orders to file
python -m microapiclient generateorder --order-count 1000 --task-count 32
  • Bulk write Orders from file to DB
python -m microapiclient postorders --order-file orders.csv --task-count 32
  • View logs in client.log
cat client.log | more

🟩 Deployment

Edit Dockerfile for each Microservice and deploy container

🟩 Versioning

Using SemVer for versioning. For the versions available, see the tags on this repository.

🟩 Authors

  • Zakhar Bengart - Initial work - Ze

See also the list of contributors who participated in this project.

🟩 License

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

About

Micro Services with FastAPI and Docker

License:GNU General Public License v3.0


Languages

Language:Python 98.1%Language:Dockerfile 1.9%