What
FastAPI Hive Framework, modulization of code layout, decoupling codes into cornerstones and endpoints, developer-friendly, easy to be integrated
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.
Requirements
Python 3.7+
FastAPI Hive Framework stands on the shoulders of giants:
- FastAPI web framework.
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
-
Duplicate the
.env.example
file and rename it to.env
-
In the
.env
file configure theAPI_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 example app with:
production running command:
uvicorn example.main:app
developing running command:
uvicorn example.main:app --reload
-
Go to http://localhost:8000/docs.
-
Click
Authorize
and enter the API key as created in the Setup step. -
You can use the sample payload from the
docs/sample_payload.json
file when trying out the house price prediction model using the API.
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/*