Skip to main content

FastAPI gateway for microservices.

Project description

https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/ https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/ https://scrutinizer-ci.com/g/dotX12/fastapi-gateway/ https://badge.fury.io/py/fastapi-gateway https://pepy.tech/project/fastapi-gateway https://pepy.tech/project/fastapi-gateway https://github.com/dotX12/fastapi-gateway/blob/master/LICENSE

⚙️ fastapi-gateway is async single entry point for microservices.

API Gateway performs many tasks: accepts, processes and distributes requests, controls traffic, monitors and controls access and security, caching, throttling.

Initially, this project was created for myself, I needed to implement identification, authentication and authorization. In the future, there was a need to limit requests for each user on every endpoint, create API plans. There were a lot of microservices and to keep in each microservice the logic for limiting endpoints, security logic, logging etc. - meaningless. Therefore, all this functionality is located at a single entry point, which already implements all the necessary tasks with security, limiting, etc., while microservices now directly solve their tasks.

💿 Installation

pip install fastapi_gateway

❗️ Benchmark

1.5k - 3k RPC.

gitshit@git ~ % wrk -t 4 -c 40 http://gateway.localtest.me:8003/gateway_endpoint/path_param/12
Running 10s test @ http://gateway.localtest.me:8003/gateway_endpoint/path_param/12
  4 threads and 40 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    30.11ms   33.65ms 312.55ms   95.98%
    Req/Sec   395.03    218.80     0.89k    73.21%
  15550 requests in 10.05s, 2.31MB read
Requests/sec:   1547.81
Transfer/sec:    235.81KB

💻 Example

Example of use (long code)
from starlette import status
from starlette.requests import Request
from starlette.responses import Response
from fastapi_gateway import route
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi import Depends
from fastapi.security import APIKeyHeader
from starlette import status
from starlette.exceptions import HTTPException

app = FastAPI(title='API Gateway')
SERVICE_URL = "http://microservice.localtest.me:8002"

API_KEY_NAME = "x-api-key"

api_key_header = APIKeyHeader(
    name=API_KEY_NAME,
    auto_error=False
)


def check_api_key(key: str = Depends(api_key_header)):
    if key:
        return key
    raise HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="You didn't pass the api key in the header! Header: x-api-key",
    )


class FooModel(BaseModel):
    example_int: int
    example_str: str


@route(
    request_method=app.post,
    service_url=SERVICE_URL,
    gateway_path='/query_and_body_path/{path}',
    service_path='/v1/query_and_body_path/{path}',
    query_params=['query_int', 'query_str'],
    body_params=['test_body'],
    status_code=status.HTTP_200_OK,
    tags=['Query', 'Body', 'Path'],
    dependencies=[
        Depends(check_api_key)
    ],
)
async def check_query_params_and_body(
        path: int, query_int: int, query_str: str,
        test_body: FooModel, request: Request, response: Response
):
    pass

See more examples here:

Tests and instructions for launch
Souce code Gateway
Souce code Microservice #1

🪛 How to use?

  • request_method - is a callable (like app.get, app.post, foo_router.patch and so on.).
  • service_url - the path to the endpoint on another service (like "https://microservice1.example.com").
  • service_path - the path to the method in microservice (like "/v1/microservice/users").
  • gateway_path - is the path to bind gateway.
    For example, your gateway api is located here - https://gateway.example.com and the path to endpoint (gateway_path) - "/users" then the full way to this method will be - https://gateway.example.com/users
  • override_headers - Boolean value allows you to return all the headlines that were created by microservice in gateway.
  • query_params - used to extract query parameters from endpoint and transmission to microservice
  • form_params - used to extract form model parameters from endpoint and transmission to microservice
  • param body_params - used to extract body model from endpoint and transmission to microservice

⚠️ - Be sure to transfer the name of the argument to the router, which is in the endpoint func!

query_params - List[Query]
body_params - List[Body]
form_params - List[File, Form]
In more detail how to transmit body, form and query (photo)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_gateway-0.0.3.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_gateway-0.0.3-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_gateway-0.0.3.tar.gz.

File metadata

  • Download URL: fastapi_gateway-0.0.3.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Linux/5.15.0-1022-azure

File hashes

Hashes for fastapi_gateway-0.0.3.tar.gz
Algorithm Hash digest
SHA256 23fb623901b2e01b19ab70a3a7cf0913d1ed8c563d06de4d4e7789fbf4dc46b0
MD5 a9a75557232d14a067bb8b89c84ad0a5
BLAKE2b-256 d6a5205fed98f0d0695d98ec1cf8b76b4877f11e71c6c869230da3a064b2b735

See more details on using hashes here.

File details

Details for the file fastapi_gateway-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: fastapi_gateway-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Linux/5.15.0-1022-azure

File hashes

Hashes for fastapi_gateway-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6e699d0a6e4c7336ce0cecf052f6349d71f0bca720bf50ac1fb528c311bde273
MD5 d1fccb58ed6b03994d4fa49ee7395498
BLAKE2b-256 859bdb512c3b4dfecd5ee3c5d024a3c336a248831f451c42350a85b38bc04de7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page