tianruci commited on
Commit
fe44f75
·
verified ·
1 Parent(s): 0f252cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -6,8 +6,14 @@ from fastapi.responses import StreamingResponse
6
  from datetime import datetime, timedelta
7
  from starlette.routing import Mount # 用于挂载
8
  from fastmcp.server import FastMCP
 
9
 
10
- app = FastAPI()
 
 
 
 
 
11
 
12
  # 缓存最新获取的热门项目
13
  cached_trending = []
@@ -63,9 +69,7 @@ def get_trending_repos(num: int = 10) -> dict:
63
  fetch_github_trending()
64
  return {"trending": cached_trending[:num]}
65
 
66
- app.mount("/mcp", mcp_server.sse_app)
67
- #app.mount("/", mcp_server.sse_app)
68
- #app.mount("/mcp", mcp_server.create_streamable_app())
69
 
70
  async def trending_generator():
71
  """SSE 事件生成器"""
@@ -89,9 +93,4 @@ async def get_trending_sse():
89
  trending_generator(),
90
  media_type="text/event-stream",
91
  headers={"Cache-Control": "no-cache", "Connection": "keep-alive"}
92
- )
93
-
94
- @app.on_event("startup")
95
- async def startup_event():
96
- """启动时初始化数据"""
97
- await fetch_github_trending()
 
6
  from datetime import datetime, timedelta
7
  from starlette.routing import Mount # 用于挂载
8
  from fastmcp.server import FastMCP
9
+ from contextlib import asynccontextmanager
10
 
11
+ @asynccontextmanager
12
+ async def lifespan(app: FastAPI):
13
+ await fetch_github_trending() # 启动时初始化
14
+ yield
15
+
16
+ app = FastAPI(lifespan=lifespan)
17
 
18
  # 缓存最新获取的热门项目
19
  cached_trending = []
 
69
  fetch_github_trending()
70
  return {"trending": cached_trending[:num]}
71
 
72
+ app.mount("/mcp", mcp_server.create_streamable_app())
 
 
73
 
74
  async def trending_generator():
75
  """SSE 事件生成器"""
 
93
  trending_generator(),
94
  media_type="text/event-stream",
95
  headers={"Cache-Control": "no-cache", "Connection": "keep-alive"}
96
+ )