- Add docstrings to models and main.py - Create API.md with detailed endpoint documentation - Create CHANGELOG.md for version tracking - Enhance FastAPI app metadata (description, version, tags) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
"""FastAPI 应用入口。
|
|
|
|
提供健康检查和项目 CRUD 端点。
|
|
"""
|
|
from fastapi import FastAPI, HTTPException
|
|
from typing import Dict
|
|
from app.models import Item, ItemCreate
|
|
|
|
app = FastAPI(
|
|
title="FastAPI Example",
|
|
description="最小化 CRUD API 示例",
|
|
version="1.0.0"
|
|
)
|
|
|
|
# 内存存储
|
|
_items: Dict[int, Item] = {}
|
|
_item_id_counter = 0
|
|
|
|
|
|
@app.get("/health", tags=["系统"])
|
|
async def health_check():
|
|
"""健康检查端点,返回服务状态。"""
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.post("/items", response_model=Item, tags=["项目"])
|
|
async def create_item(item: ItemCreate):
|
|
"""创建新项目。"""
|
|
global _item_id_counter
|
|
_item_id_counter += 1
|
|
new_item = Item(id=_item_id_counter, **item.model_dump())
|
|
_items[_item_id_counter] = new_item
|
|
return new_item
|
|
|
|
|
|
@app.get("/items", response_model=list[Item], tags=["项目"])
|
|
async def list_items():
|
|
"""列出所有项目。"""
|
|
return list(_items.values())
|
|
|
|
|
|
@app.get("/items/{item_id}", response_model=Item, tags=["项目"])
|
|
async def get_item(item_id: int):
|
|
"""获取单个项目。
|
|
|
|
Args:
|
|
item_id: 项目 ID
|
|
|
|
Raises:
|
|
HTTPException: 项目不存在时返回 404
|
|
"""
|
|
if item_id not in _items:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return _items[item_id] |