Skip to content

What

architecture

startup_flow

FastAPI Hive Framework, modulization of code layout, decoupling codes into cornerstones and endpoints, developer-friendly, easy to be integrated

build codecov Supported Versions PyPI version License Downloads

Introduction:

Regular fastapi project setups some folders for storing specific function codes, such as router folder for api registering folder and model folder for defining db tables and pydantic data model.

So one service codes have to be splitted and exist in several folders, the problem is the code-review issue of looking for them with jumpping different folders again and again.

If you feel it is tiresome task like me, then FastAPI Hive is just for you.

Every bee is thought as the entity of one service codes, defined as endpoints, including all function codes(router, model). FastAPI Hive is the container(bee's home) for all bees.

Also for these common function code, like database setup and authentication codes, they are defined as cornerstones, which are key to construct bee's hive, every function code are put into one cornerstone folder together.


Documentation: https://fanqingsong.github.io/fastapi-hive

Source Code: https://github.com/fanqingsong/fastapi-hive

PYPI: https://pypi.org/project/fastapi-hive/


FastAPI Hive Framework is a developer friendly and easy to be integrated framework for managing your code by endpoints and cornerstones folder structure.

The key features are:

  • Conerstone Container: a top-level folder to layout codes by function folder, like db and authentication.
  • Endpoint Container: a top-level folder to layout service codes by endpoint folder,
  • Endpoint folder: a sub-folder in Endpoint Container, layout one service code by function folder(router, db, service, etc).
  • Router Mounting Automatically: the router defined by module will be mounted into app automatically.
  • Model Preloading Easily: the service(such as ML model) defined by module will be mounted into app easily, in order to reduce loading time during endpoint request.
  • Developer-Friendly: all one-endpoint/cornerstone codes are put in one same folders, easy to review and update.
  • Easy-to-be-Integrated: Just several line codes to integrate it in your app.

estimation based on tests by author with this project**, have a look at example folder.

Overview

Folders are set in such layout, urls are mapped by folder structure automatically.

architecture

Requirements

Python 3.7+

FastAPI Hive Framework stands on the shoulders of giants:

Have a Try

Installation

install hive from pypi

pip3 install fastapi_hive

install hive from source code

First, git clone this repo.

git clone git@github.com:fanqingsong/fastapi-hive.git

Second, install ioc framework library of this container_name
Note: If you just treat it as a library, and want to integrate it into your app, you can just run this command.

pip3 install .

install dependency packages

Third, install the required packages for running demo in your local environment (ideally virtualenv, conda, etc.). Note: You can reference demo code to make up your own app in anywhere.

pip3 install -r requirements.txt

Setup

  1. Duplicate the .env.example file and rename it to .env

  2. In the .env file configure the API_KEY entry. The key is used for authenticating our API.
    A sample API key can be generated using Python REPL:

import uuid
print(str(uuid.uuid4()))

Run app

  1. Start example app with:

production running command:

uvicorn example.main:app

developing running command:

uvicorn example.main:app --reload
  1. Go to http://localhost:8000/docs.

  2. Click Authorize and enter the API key as created in the Setup step. Authroization

  3. You can use the sample payload from the docs/sample_payload.json file when trying out the house price prediction model using the API. Prediction with example payload

Run Tests

If you're not using tox, please install with:

pip3 install tox

Run your tests with:

tox

This runs tests and coverage for Python 3.6 and Flake8, Autopep8, Bandit.

Package and Upload

For maintainer of this project, please follow: Before these action, change version in setup.py

python3 setup.py sdist

twine upload dist/*