This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
fromfastapiimportFastAPI
fromfastapi.middleware.corsimportCORSMiddleware
fromapi.routerimportrouter,tq_service
importuvicorn
importargparse
app=FastAPI(
title="TQAPI Service",
description="天勤量化 API 服务",
version="1.0.0"
)
# 配置 CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],# 在生产环境中应该设置具体的域名
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
app.include_router(router,prefix="/api")
# 健康检查
@app.get("/health")
asyncdefhealth_check():
return{"status":"healthy"}
# 关闭事件处理
@app.on_event("shutdown")
asyncdefshutdown_event():
"""服务关闭时的清理工作"""
print("正在关闭服务...")
# 断开TQApi连接
try:
success=awaittq_service.disconnect()
ifsuccess:
print("TQApi连接已成功关闭")
else:
print("TQApi连接关闭失败")
exceptExceptionase:
print(f"关闭TQApi连接时出错: {e}")
print("服务已关闭")
if__name__=="__main__":
# 解析命令行参数
parser=argparse.ArgumentParser(description="天勤量化 API 服务")