main
HuangHai 4 months ago
parent 9265818e27
commit 0dcb641f27

@ -21,6 +21,8 @@ app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
vn = VannaUtil()
# 初始化 FastAPI 应用
@asynccontextmanager
async def lifespan(app: FastAPI):
@ -38,15 +40,19 @@ async def lifespan(app: FastAPI):
# 关闭时释放连接池
await app.state.pool.close()
app = FastAPI(lifespan=lifespan)
# 依赖注入连接池
async def get_db():
async with app.state.pool.acquire() as connection:
yield connection
@app.post("/questions/get_excel")
async def get_excel(question_id: str = Form(...), question_str: str = Form(...), db: asyncpg.Connection = Depends(get_db)):
async def get_excel(question_id: str = Form(...), question_str: str = Form(...),
db: asyncpg.Connection = Depends(get_db)):
# 只接受guid号
if len(question_id) != 36:
return {"success": False, "message": "question_id格式错误"}
@ -88,6 +94,7 @@ client = AsyncOpenAI(
base_url=MODEL_API_URL,
)
@app.api_route("/questions/get_docx_stream", methods=["POST", "GET"])
async def get_docx_stream(
question_id: str = Form(None, description="问题IDPOST请求"), # POST 请求参数
@ -115,7 +122,7 @@ async def get_docx_stream(
5数据
'''
_data = await db.fetch(sql)
#print(_data)
# print(_data)
# 将 asyncpg.Record 转换为 JSON 格式
json_data = json.dumps([dict(record) for record in _data], ensure_ascii=False)
print(json_data) # 打印 JSON 数据
@ -215,6 +222,7 @@ def set_system_recommend(question_id: str = Form(...), flag: str = Form(...)):
# 提示保存成功
return {"success": True, "message": "保存成功"}
# 设置问题为用户收藏问题 ,0:取消1设置
@app.post("/questions/set_user_collect")
def set_user_collect(question_id: str = Form(...), flag: str = Form(...)):
@ -222,6 +230,7 @@ def set_user_collect(question_id: str = Form(...), flag: str = Form(...)):
# 提示保存成功
return {"success": True, "message": "保存成功"}
# 查询有哪些系统推荐问题
@app.get("/questions/get_system_recommend")
def get_system_recommend():
@ -230,6 +239,7 @@ def get_system_recommend():
# 返回查询结果
return {"success": True, "data": system_recommend_questions}
# 查询有哪些用户收藏问题
@app.get("/questions/get_user_collect")
def get_user_collect():
@ -237,6 +247,8 @@ def get_user_collect():
user_collect_questions = get_user_collect_questions()
# 返回查询结果
return {"success": True, "data": user_collect_questions}
# 启动 FastAPI
if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True, workers=4)

Loading…
Cancel
Save