# seatools-starter-web-fastapi **Repository Path**: seatools-py/seatools-starter-web-fastapi ## Basic Information - **Project Name**: seatools-starter-web-fastapi - **Description**: seatools ioc fastapi 启动器 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-13 - **Last Updated**: 2025-05-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: Python, ioc, Spring, starter ## README # 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`如下: ```yaml seatools: server: # 此处为uvicorn参数配置 uvicorn: host: 0.0.0.0 port: 8000 workers: 1 reload: true # 此处为fastapi配置 fastapi: title: xxxxx description: xxx docs_url: none ``` 3. 使用, 通过定义ioc容器函数加载 ```python from seatools.ioc import Autowired, Bean from fastapi import FastAPI app = Autowired(cls=FastAPI) # Add middleware from fastapi.middleware.cors import CORSMiddleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Add global exception handler # Custom exception handling @app.exception_handler(AssertionError) async def assert_exception_handler(request, exc): ... # Add route method @app.get('/') async def hello(): return "hello" # Add route 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 integration with seatools ioc injection @Bean class ServiceA: async def hello(self): return "serviceA" @app.get('/serviceA') async def serviceA(serviceA = Autowired(cls=ServiceA)): # Inject seatools.ioc in the controller parameter using Autowired, note that the type should not be explicitly declared here, FastAPI will fail on parameter type checking for unsupported types return await serviceA.hello() # Or inject at the router level, this method is more recommended @app.get('/serviceA') async def serviceA(): serviceA = Autowired(cls=ServiceA) return await serviceA.hello() ``` 3. 运行, 具体见`seatools-starter-server-*`, [`seatools-starter-server-uvicorn`](https://gitee.com/seatools-py/seatools-starter-server-uvicorn)