# seatools fastapi 启动器
该框架必须和`seatools-starter-server-*`的包集成配合使用, 这里以`seatools-starter-server-uvicorn`为例
## 使用指南
1. 安装, `poetry add 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
# 添加中间件,
@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()
```
Raw data
{
"_id": null,
"home_page": null,
"name": "seatools-starter-web-fastapi",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "seatools, starter",
"author": null,
"author_email": "seatools-py <521274311@qq.com>",
"download_url": "https://files.pythonhosted.org/packages/7a/24/ceb9d83654f114b8d5afb31e6d6d317f4a6c1f16dc112589321fb567f0df/seatools_starter_web_fastapi-1.0.0.tar.gz",
"platform": null,
"description": "# seatools fastapi \u542f\u52a8\u5668\n\n\u8be5\u6846\u67b6\u5fc5\u987b\u548c`seatools-starter-server-*`\u7684\u5305\u96c6\u6210\u914d\u5408\u4f7f\u7528, \u8fd9\u91cc\u4ee5`seatools-starter-server-uvicorn`\u4e3a\u4f8b\n\n## \u4f7f\u7528\u6307\u5357\n1. \u5b89\u88c5, `poetry add seatools-starter-server-uvicorn seatools-starter-web-fastapi`\n2. \u914d\u7f6e`config/application.yml`\u5982\u4e0b:\n```yaml\n\nseatools:\n server:\n # \u6b64\u5904\u4e3auvicorn\u53c2\u6570\u914d\u7f6e\n uvicorn:\n host: 0.0.0.0\n port: 8000\n workers: 1\n reload: true\n # \u6b64\u5904\u4e3afastapi\u914d\u7f6e\n fastapi:\n title: xxxxx\n description: xxx\n docs_url: none\n```\n3. \u4f7f\u7528, \u901a\u8fc7\u5b9a\u4e49ioc\u5bb9\u5668\u51fd\u6570\u52a0\u8f7d\n```python\nfrom seatools.ioc import Autowired, Bean\nfrom fastapi import FastAPI\n\n# \u6dfb\u52a0\u4e2d\u95f4\u4ef6, \n@Bean\ndef xxx_middleware(app: FastAPI):\n from fastapi.middleware.cors import CORSMiddleware\n \n app.add_middleware(\n CORSMiddleware,\n allow_origins=[\"*\"],\n allow_credentials=True,\n allow_methods=[\"*\"],\n allow_headers=[\"*\"],\n)\n\n# \u6dfb\u52a0\u5168\u5c40\u5f02\u5e38\u5904\u7406\n@Bean\ndef global_exception_handler(app: FastAPI):\n \n # \u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\n @app.exception_handler(AssertionError)\n async def assert_exception_handler(request, exc):\n ...\n\n# \u6dfb\u52a0\u8def\u7531\u65b9\u6cd5\n@Bean\ndef xxx_router(app: FastAPI):\n \n @app.get('/')\n async def hello():\n return \"hello\"\n\n# \u6dfb\u52a0\u8def\u7531\nfrom fastapi import APIRouter\ncustom_router = APIRouter(prefix='/custom')\n\n@custom_router.get('/')\nasync def custom_hello():\n return \"custom hello\"\n\n@Bean\ndef register_custom_router(app: FastAPI):\n app.include_router(custom_router)\n\n\n# fastapi \u4e0e seatools \u7684\u96c6\u6210\u6ce8\u5165\n@Bean\nclass ServiceA:\n \n async def hello(self):\n return \"serviceA\"\n\n\n@Bean\ndef a_router(app: FastAPI):\n \n @app.get('/serviceA')\n async def serviceA(serviceA = Autowired(cls=ServiceA)): # \u5728\u63a7\u5236\u5668\u53c2\u6570\u4e2d\u4f7f\u7528Autowired\u65b9\u5f0f\u6ce8\u5165seatools.ioc, \u6ce8\u610f\u6b64\u5904\u4e0d\u80fd\u4e3b\u52a8\u58f0\u660e\u7c7b\u578b, fastapi\u4f1a\u5bf9\u53c2\u6570\u7c7b\u578b\u6821\u9a8c\u4e0d\u652f\u6301\u7684\u7c7b\u578b\u5c06\u5931\u8d25\n return await serviceA.hello()\n \n# \u6216\u8005\u5728router\u5c42\u6ce8\u5165, \u66f4\u63a8\u8350\u8be5\u65b9\u5f0f\n\n@Bean\ndef a2_router(app: FastAPI, serviceA: ServiceA): # \u5177\u4f53\u6ce8\u5165\u65b9\u5f0f\u89c1seatools\n\n @app.get('/serviceA')\n async def serviceA():\n return await serviceA.hello()\n```",
"bugtrack_url": null,
"license": "MIT",
"summary": "Seatools Starter Web Fastapi",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/seatools-py/seatools-starter-web-fastapi",
"Issues": "https://github.com/seatools-py/seatools-starter-web-fastapi/issues"
},
"split_keywords": [
"seatools",
" starter"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c59b85968b0303dd32b84dda007609924281fc735369bdab336651aa861022e0",
"md5": "9c96a6e898c73209e196d2a6a5f4f46c",
"sha256": "2e6da3aa0db5dc5d43de37b02d45bfcdf34bd42b888305826337b35c413af9db"
},
"downloads": -1,
"filename": "seatools_starter_web_fastapi-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9c96a6e898c73209e196d2a6a5f4f46c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 4175,
"upload_time": "2024-12-13T04:39:52",
"upload_time_iso_8601": "2024-12-13T04:39:52.185215Z",
"url": "https://files.pythonhosted.org/packages/c5/9b/85968b0303dd32b84dda007609924281fc735369bdab336651aa861022e0/seatools_starter_web_fastapi-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7a24ceb9d83654f114b8d5afb31e6d6d317f4a6c1f16dc112589321fb567f0df",
"md5": "79f75576f19d403e97d2b06bf6fe7d53",
"sha256": "413d299307f9895e5a33b27c2d26b85b6a2de3be354a4699a50ed6aa626a55ef"
},
"downloads": -1,
"filename": "seatools_starter_web_fastapi-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "79f75576f19d403e97d2b06bf6fe7d53",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 3186,
"upload_time": "2024-12-13T04:39:55",
"upload_time_iso_8601": "2024-12-13T04:39:55.470916Z",
"url": "https://files.pythonhosted.org/packages/7a/24/ceb9d83654f114b8d5afb31e6d6d317f4a6c1f16dc112589321fb567f0df/seatools_starter_web_fastapi-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-13 04:39:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "seatools-py",
"github_project": "seatools-starter-web-fastapi",
"github_not_found": true,
"lcname": "seatools-starter-web-fastapi"
}