tianruci commited on
Commit
e293967
·
verified ·
1 Parent(s): 7dd8366

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -4,17 +4,23 @@ import requests
4
  from fastapi import FastAPI, Response
5
  from fastapi.responses import StreamingResponse
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
  cached_trending = []
19
  last_updated = None
20
 
@@ -40,6 +46,7 @@ async def fetch_github_trending():
40
  response.raise_for_status()
41
  items = response.json().get("items", [])
42
 
 
43
  trending_projects = []
44
  for item in items:
45
  trending_projects.append({
@@ -57,7 +64,7 @@ async def fetch_github_trending():
57
  print(f"Error fetching GitHub trending: {e}")
58
  return cached_trending if cached_trending else []
59
 
60
- #mcp_server = FastMCP(name="GithubTrending", stateless_http=True)
61
  mcp_server = FastMCP(name="GithubTrending")
62
 
63
  @mcp_server.tool()
@@ -67,11 +74,10 @@ def get_trending_repos(num: int = 10) -> dict:
67
  fetch_github_trending()
68
  return {"trending": cached_trending[:num]}
69
 
70
- #app.mount("/mcp", mcp_server.streamable_http_app())
71
- #app.mount("/mcp", mcp_server.http_app())
72
- app.mount("/", mcp_server.http_app())
73
 
74
  async def trending_generator():
 
75
  while True:
76
  if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
77
  await fetch_github_trending()
@@ -80,13 +86,14 @@ async def trending_generator():
80
 
81
  @app.get("/trending")
82
  async def get_trending():
 
83
  if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
84
  await fetch_github_trending()
85
  return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
86
 
87
-
88
  @app.get("/trending-sse")
89
  async def get_trending_sse():
 
90
  return StreamingResponse(
91
  trending_generator(),
92
  media_type="text/event-stream",
 
4
  from fastapi import FastAPI, Response
5
  from fastapi.responses import StreamingResponse
6
  from datetime import datetime, timedelta
7
+ from starlette.routing import Mount # 用于挂载
8
  from fastmcp.server import FastMCP
9
  from contextlib import asynccontextmanager
10
+ import logging
11
+
12
+ # 启用 FastMCP 日志调试
13
+ logging.basicConfig(level=logging.DEBUG)
14
 
15
  @asynccontextmanager
16
  async def lifespan(app: FastAPI):
17
+ await fetch_github_trending() # 启动时初始化
18
  yield
19
 
20
+ # 添加 root_path 支持 HF Spaces 代理
21
+ app = FastAPI(lifespan=lifespan, root_path=os.environ.get("ROOT_PATH", ""))
22
 
23
+ # 缓存最新获取的热门项目
24
  cached_trending = []
25
  last_updated = None
26
 
 
46
  response.raise_for_status()
47
  items = response.json().get("items", [])
48
 
49
+ # 简化项目信息
50
  trending_projects = []
51
  for item in items:
52
  trending_projects.append({
 
64
  print(f"Error fetching GitHub trending: {e}")
65
  return cached_trending if cached_trending else []
66
 
67
+ # 定义MCP服务器和工具
68
  mcp_server = FastMCP(name="GithubTrending")
69
 
70
  @mcp_server.tool()
 
74
  fetch_github_trending()
75
  return {"trending": cached_trending[:num]}
76
 
77
+ app.mount("/mcp", mcp_server.http_app())
 
 
78
 
79
  async def trending_generator():
80
+ """SSE 事件生成器"""
81
  while True:
82
  if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
83
  await fetch_github_trending()
 
86
 
87
  @app.get("/trending")
88
  async def get_trending():
89
+ """原有端点"""
90
  if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
91
  await fetch_github_trending()
92
  return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
93
 
 
94
  @app.get("/trending-sse")
95
  async def get_trending_sse():
96
+ """原有SSE端点"""
97
  return StreamingResponse(
98
  trending_generator(),
99
  media_type="text/event-stream",