Skip to main content

Seatools Starter Web Fastapi

Project description

seatools fastapi 启动器

该框架必须和seatools-starter-server-*的包集成配合使用, 这里以seatools-starter-server-uvicorn为例

使用指南

  1. 安装, poetry add fastapi seatools-starter-server-uvicorn seatools-starter-web-fastapi
  2. 配置config/application.yml如下:
seatools:
  server:
    # 此处为uvicorn参数配置
    uvicorn:
      host: 0.0.0.0
      port: 8000
      workers: 1
      reload: true
  # 此处为fastapi配置
  fastapi:
    title: xxxxx
    description: xxx
    docs_url: none
  1. 使用, 通过定义ioc容器函数加载
from seatools.ioc import Autowired, Bean
from fastapi import FastAPI

# 添加中间件, 
@Bean
def xxx_middleware(app: FastAPI):
    from fastapi.middleware.cors import CORSMiddleware
    
    app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 添加全局异常处理
@Bean
def global_exception_handler(app: FastAPI):
    
    # 自定义异常处理
    @app.exception_handler(AssertionError)
    async def assert_exception_handler(request, exc):
        ...

# 添加路由方法
@Bean
def xxx_router(app: FastAPI):
    
    @app.get('/')
    async def hello():
        return "hello"

# 添加路由
from fastapi import APIRouter
custom_router = APIRouter(prefix='/custom')

@custom_router.get('/')
async def custom_hello():
    return "custom hello"

@Bean
def register_custom_router(app: FastAPI):
    app.include_router(custom_router)


# fastapi 与 seatools 的集成注入
@Bean
class ServiceA:
    
    async def hello(self):
        return "serviceA"


@Bean
def a_router(app: FastAPI):
    
    @app.get('/serviceA')
    async def serviceA(serviceA = Autowired(cls=ServiceA)): # 在控制器参数中使用Autowired方式注入seatools.ioc, 注意此处不能主动声明类型, fastapi会对参数类型校验不支持的类型将失败
        return await serviceA.hello()
    
# 或者在router层注入, 更推荐该方式

@Bean
def a2_router(app: FastAPI, serviceA: ServiceA): # 具体注入方式见seatools

    @app.get('/serviceA')
    async def serviceA():
        return await serviceA.hello()
  1. 运行, 具体见seatools-starter-server-*, seatools-starter-server-uvicorn

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seatools_starter_web_fastapi-1.0.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

seatools_starter_web_fastapi-1.0.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file seatools_starter_web_fastapi-1.0.1.tar.gz.

File metadata

File hashes

Hashes for seatools_starter_web_fastapi-1.0.1.tar.gz
Algorithm Hash digest
SHA256 848b7c1a5e51105981d87ddf1b47871f92c9c9d632f15ad8000d1af94f5548c8
MD5 4586b8fe8dc79b4f159c3e4b52eebfdd
BLAKE2b-256 4694f6980e0ccc03e3dd246ed8e8116bdf7f20efb289ebe929255100d3c9ed55

See more details on using hashes here.

File details

Details for the file seatools_starter_web_fastapi-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for seatools_starter_web_fastapi-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d8bccbf442758747dca837c90b66215f51c91218b3472037f6d835d69f845d3
MD5 73d9bf73c005d6e1bc75584fca03d88b
BLAKE2b-256 5ba382093f02ef1374cc909de9e3d031bafcb67ee9d9b0ff1011f26856c83848

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page