fastapi-boot 0.0.50
FastAPI tools
Requires Python: >=3.13

<h1 align="center">FastAPIBoot</h1>


<div align="center">

[![test](https://github.com/hfdy0935/fastapi-boot/actions/workflows/test.yml/badge.svg)](https://github.com/hfdy0935/fastapi-boot/actions/workflows/test.yml)
[![build&publish](https://github.com/hfdy0935/fastapi-boot/actions/workflows/build_publish.yml/badge.svg)](https://github.com/hfdy0935/fastapi-boot/actions/workflows/build_publish.yml)
[![coverage](https://img.shields.io/codecov/c/github/hfdy0935/fastapi-boot)](https://codecov.io/gh/hfdy0935/fastapi-boot)
[![pypi](https://img.shields.io/badge/pypi-v0.0.50-blue
)](https://pypi.org/project/fastapi-boot/)
[![support-version](https://img.shields.io/badge/python-3.10%2B-orange)]()

</div>




&emsp;&emsp;简单易用、功能强大的FastAPI工具库，支持CBV、依赖注入、声明式公共路由依赖和生命周期等写法，为**提高效率**而生。
> cbv: class based view

**特点**
- 📦**无缝集成FastAPI，开箱即用**，继承FastAPI的优点，支持通过`CLI`初始化。
- 🐎**支持`CBV`、`FBV`**，想套多少层就套多少层，路由层级关系更清晰。
- ✅ **践行`IOC`、`DI`、`AOP`**，开发更高效。
- 🌈**公共依赖提取**，结合多层`CBV`，避免`endpoint`中大量的`Depends`。
- 🔨**丰富的工具**，生命周期、异常处理、中间件、`tortoise`工具。


## 1. 快速开始

### 1.1 安装 
```bash
pip install fastapi-boot
# 或者使用uv
uv add fastapi-boot
```

### 1.2 和FastAPI比较
📌要实现这些接口
<img src="https://raw.githubusercontent.com/hfdy0935/fastapi-boot/refs/heads/main/assets/image.png"/>

1. 用fastapi_boot
```py
from typing import Annotated
from fastapi import Query
from fastapi_boot.core import Controller, Get, provide_app, Post
import uvicorn


# fbv, function based view
@Get('/r1')
def top_level_fbv1():
    return '/r1'


# fbv
@Controller('/r2').get('')
def top_level_fbv2():
    return '/r2'


# cbv, class based view
@Controller('/r3')
class CBVController:
    @Get('/1')
    async def cbv_endpoint1(self):
        return '/r3/1'

    @Post('/2')
    def cbv_endpoint2(self, q: Annotated[str, Query()]):
        return dict(query=q, path='/r3/2')


app = provide_app(controllers=[top_level_fbv1, top_level_fbv2, CBVController])


if __name__ == '__main__':
    uvicorn.run('main:app', reload=True)
```

- 用fastapi

```py
from typing import Annotated
from fastapi import APIRouter, FastAPI, Query
import uvicorn


app = FastAPI()

@app.get('/r1')
def endpoint1():
    return '/r1'


router1 = APIRouter(prefix='/r2')

@router1.get('')
def endpoint2():
    return '/r2'

app.include_router(router1)


router2 = APIRouter(prefix='/r3')

@router2.get('/1')
async def endpoint3():
    return '/r3/1'

@router2.post('/2')
def endpoint4(q: Annotated[str, Query()]):
    return dict(query=q, path='/r3/2')

app.include_router(router2)


if __name__ == '__main__':
    uvicorn.run('main:app', reload=True)
```


### 1.3  💡通过CLI生成：
```bash
fastapi-boot --host=localhost --port=8000 --reload --name=Demo
```
<img src="https://raw.githubusercontent.com/hfdy0935/fastapi-boot/refs/heads/main/assets/image-1.png"/>


## 2. 所有API

```py
from fastapi_boot.core import (
    Injectable,
    Bean,
    provide_app, 
    use_dep, 
    use_http_middleware, 
    use_ws_middleware, 
    inject.
    Controller,
    Delete,
    Get,
    Head,
    Options,
    Patch,
    Post,
    Prefix,
    Put,
    Req,
    Trace,
    WebSocket as WS,
)

# tortoise工具
from fastapi_boot.tortoise_util import Sql, Select, Update, Insert, Delete as SqlDelete
```