为什么选 FastAPI?
- 基于 Pydantic 的自动数据验证
- 自动生成 Swagger UI 和 ReDoc 文档
- 原生支持 async/await,性能媲美 Go/Node
快速开始
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.post("/items/")
async def create_item(item: Item):
return {"item": item, "status": "created"}部署
使用 uvicorn 运行:uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4,生产环境建议配合 Nginx 反代。