tianruci commited on
Commit
832b35a
·
verified ·
1 Parent(s): f296792

Update app.py

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