Git Product home page Git Product logo

flaskapi's Introduction

logo

description

๐ŸŸช Getting Started

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

Requirements

Flask Docker MongoDB Nginx

Installing

Clone the project

git clone [email protected]:zakharb/flaskapi.git
cd flaskapi

Start docker-compose

docker-compose up -d

๐ŸŸช Usage

Customers

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' \

๐ŸŸช Configuration

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)

Examples

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

๐ŸŸช 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

flaskapi's People

Contributors

zakharb avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.