38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
# app/main.py
|
|
import time
|
|
import requests # ❌ 引入了同步的请求库
|
|
from fastapi import FastAPI
|
|
|
|
app = FastAPI()
|
|
|
|
# 内存数据库模拟
|
|
DATABASE = []
|
|
|
|
# ❌ 严重问题 1: 硬编码敏感信息 (有安全隐患)
|
|
SECRET_KEY = "super-secret-key-123456"
|
|
|
|
|
|
# ❌ 严重问题 2: 在 async 异步函数中,使用了同步阻塞操作 (time.sleep 和 requests.get)
|
|
# 这会导致 FastAPI 的单线程事件循环被完全卡死!高并发时性能会雪崩 [1][3]。
|
|
@app.get("/fetch-data")
|
|
async def fetch_external_data():
|
|
time.sleep(2) # 模拟耗时操作,卡死事件循环 [3]
|
|
response = requests.get("https://api.github.com/repos/tiangolo/fastapi") # 阻塞 I/O [1]
|
|
return response.json()
|
|
|
|
|
|
# ❌ 严重问题 3: 没有任何数据校验
|
|
# 客户端如果少传了 name 或者 price 字段,直接报 KeyError 500 内部错误。
|
|
@app.post("/items")
|
|
async def create_item(item: dict): # ❌ 应该使用 Pydantic BaseModel [1][3]
|
|
item_name = item["name"]
|
|
item_price = item["price"]
|
|
|
|
new_item = {"name": item_name, "price": item_price}
|
|
DATABASE.append(new_item)
|
|
return {"status": "success", "data": new_item}
|
|
|
|
# TODO: 这是一个测试 AI 审查的临时注释
|
|
# TODO: 这是一个测试 AI 审查的临时注释
|
|
return {"message": "Hello, World!"} # ❌ 这行代码永远不会被执行,应该删除
|