a lightweight web app framework based on fastapi
Version 2021, based on FastAPI, an easy-to-use web app developed upon Starlette Framework
pip3 install -r ./requirements.txt
The web application is based on onion style~
The directory structure is:
__init__.py
as entry of user modules
__init__.py
as entry of core modules, better make it able to be reused in other projects
Run ./main.py
to start the example, which includes:
The internal steps are:
./main.py
loads the configs inside ./cfg/{env}
on cmd args, then calls uvicorn.run
to start fastapi app at ./app/__init__.py
./app/__init__.py
, core modules and user handlers/models/services are loaded at startup
event of fastapi appYou can put your launch scripts inside ./misc/build
for your different launch options
To build your logic, common steps are follows:
./main.py
runs server in dev environment in default, in which hot-reload is enabled./app/handler
, add corresponding import
& APP.include_router
codes in ./app/__init__.py
./app/model
, add services in ./app/service
./app/middleware
if necessarySome tips for coding:
/docs
to test the routers on web pageasync def
functions), as it may block the main evtloop, so that other requests are not handled in time. def
functions will be invoked in different threads./core
should be shareable (for other projects), codes of ./app
should fit with current project./misc/dev/gen_code.py
to generate template codes for handlers, models & services. Exec it with working directory as project root directorypydantic.BaseModel
, it's powerfulMost of the handled requests should contain a status code of 200
A simple solution is to use Resp
model in ./core/model/handler.py
to generate response body for your handlers
{
"success": bool,
"message": str,
"code": IntEnum,
"data": Any,
}
Use Resp.ok
to generate success response and use Resp.err
to generate error response
The test handler ./app/handler/test.py
contains ws handler examples
To know more about it, see websocket documentation
Run ./misc/build/pack.sh
to pack the project into ./misc/build/start-fastapi.tar.gz
See ./misc/build/Dockerfile
for an example of docker deployment