Skip to content

What

architecture

FastAPI Hive Framework, modulization of code layout, decoupling codes into foundations 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 foundations, the structure the hive is built on. Every function code is put into one foundation 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 foundations folder structure.

The key features are:

  • Foundation 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).
  • Explicit router mounting: each endpoint includes its APIRouter from startup with self.app.include_router(...).
  • 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/foundation 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. Each endpoint chooses its URL prefix when it mounts the router.

architecture

Requirements

Python 3.8–3.13

FastAPI Hive Framework stands on the shoulders of giants:

Have a Try

Installation

Install from the local checkout. The package published on PyPI can lag behind this repository.

First, git clone this repo.

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

Second, install the library from the local source.
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 example/.env.example file and rename it to example/.env

  2. In the example/.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

Start the example from this repository so it uses the local checkout, not the PyPI release.

pip3 install -e .
pip3 install -r requirements.txt
uvicorn example.main:app --reload

The same local install and startup sequence is make run-example.

production running command:

uvicorn example.main:app
  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 the configured test, coverage, and code-quality environments.

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/*