tianruci commited on
Commit
07cb7a4
·
verified ·
1 Parent(s): 06f4f4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -13,13 +13,14 @@ async def lifespan(app: FastAPI):
13
 
14
  app = FastAPI(lifespan=lifespan)
15
 
16
- # 添加 CORS 支持
17
  app.add_middleware(
18
  CORSMiddleware,
19
  allow_origins=["*"],
20
  allow_credentials=True,
21
  allow_methods=["*"],
22
  allow_headers=["*"],
 
23
  )
24
 
25
  # 缓存设置
@@ -61,7 +62,7 @@ async def fetch_github_trending():
61
  print(f"Error fetching GitHub trending: {e}")
62
  return cached_trending if cached_trending else []
63
 
64
- # 初始化 MCP 服务器 - 使用正确的参数
65
  mcp_server = FastMCP(
66
  name="GithubTrending",
67
  stateless_http=True
@@ -72,10 +73,14 @@ def get_trending_repos(num: int = 10) -> dict:
72
  """获取 GitHub 热门仓库"""
73
  if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
74
  fetch_github_trending()
75
- return {"trending": cached_trending[:num]} # 修正拼写错误: trending -> trending
76
 
77
- # 挂载 MCP 应用 - 使用正确的路径
78
- app.mount("/mcp", mcp_server.http_app(path="/mcp"))
 
 
 
 
79
 
80
  @app.get("/trending")
81
  async def get_trending():
@@ -84,7 +89,7 @@ async def get_trending():
84
  await fetch_github_trending()
85
 
86
  return {
87
- "trending": cached_trending, # 修正拼写错误: trening -> trending
88
  "last_updated": last_updated.isoformat() if last_updated else None,
89
  "cache_expires_in": 300,
90
  "suggested_polling_interval": 60
 
13
 
14
  app = FastAPI(lifespan=lifespan)
15
 
16
+ # 更宽松的 CORS 配置
17
  app.add_middleware(
18
  CORSMiddleware,
19
  allow_origins=["*"],
20
  allow_credentials=True,
21
  allow_methods=["*"],
22
  allow_headers=["*"],
23
+ expose_headers=["*"]
24
  )
25
 
26
  # 缓存设置
 
62
  print(f"Error fetching GitHub trending: {e}")
63
  return cached_trending if cached_trending else []
64
 
65
+ # 初始化 MCP 服务器
66
  mcp_server = FastMCP(
67
  name="GithubTrending",
68
  stateless_http=True
 
73
  """获取 GitHub 热门仓库"""
74
  if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
75
  fetch_github_trending()
76
+ return {"trending": cached_trending[:num]}
77
 
78
+ # 关键修改:确保正确挂载 MCP 服务
79
+ app.mount("/mcp", mcp_server.http_app())
80
+
81
+ @app.get("/")
82
+ async def root():
83
+ return {"message": "GitHub Trending API is running"}
84
 
85
  @app.get("/trending")
86
  async def get_trending():
 
89
  await fetch_github_trending()
90
 
91
  return {
92
+ "trending": cached_trending,
93
  "last_updated": last_updated.isoformat() if last_updated else None,
94
  "cache_expires_in": 300,
95
  "suggested_polling_interval": 60