Seatools Starter Web Fastapi
Project description
seatools fastapi 启动器
该框架必须和seatools-starter-server-*的包集成配合使用, 这里以seatools-starter-server-uvicorn为例
使用指南
- 安装,
poetry add fastapi seatools-starter-server-uvicorn seatools-starter-web-fastapi - 配置
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
- 使用, 通过定义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()
- 运行, 具体见
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file seatools_starter_web_fastapi-1.0.1.tar.gz.
File metadata
- Download URL: seatools_starter_web_fastapi-1.0.1.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
848b7c1a5e51105981d87ddf1b47871f92c9c9d632f15ad8000d1af94f5548c8
|
|
| MD5 |
4586b8fe8dc79b4f159c3e4b52eebfdd
|
|
| BLAKE2b-256 |
4694f6980e0ccc03e3dd246ed8e8116bdf7f20efb289ebe929255100d3c9ed55
|
File details
Details for the file seatools_starter_web_fastapi-1.0.1-py3-none-any.whl.
File metadata
- Download URL: seatools_starter_web_fastapi-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d8bccbf442758747dca837c90b66215f51c91218b3472037f6d835d69f845d3
|
|
| MD5 |
73d9bf73c005d6e1bc75584fca03d88b
|
|
| BLAKE2b-256 |
5ba382093f02ef1374cc909de9e3d031bafcb67ee9d9b0ff1011f26856c83848
|