Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,18 @@ 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 +41,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,8 +59,8 @@ async def fetch_github_trending():
|
|
| 57 |
print(f"Error fetching GitHub trending: {e}")
|
| 58 |
return cached_trending if cached_trending else []
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
mcp_server = FastMCP(name="GithubTrending")
|
| 62 |
|
| 63 |
@mcp_server.tool()
|
| 64 |
def get_trending_repos(num: int = 10) -> dict:
|
|
@@ -67,11 +69,11 @@ def get_trending_repos(num: int = 10) -> dict:
|
|
| 67 |
fetch_github_trending()
|
| 68 |
return {"trending": cached_trending[:num]}
|
| 69 |
|
| 70 |
-
|
| 71 |
-
app.mount("/mcp",
|
| 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 +82,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 |
|
| 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 = []
|
| 20 |
last_updated = None
|
| 21 |
|
|
|
|
| 41 |
response.raise_for_status()
|
| 42 |
items = response.json().get("items", [])
|
| 43 |
|
| 44 |
+
# 简化项目信息
|
| 45 |
trending_projects = []
|
| 46 |
for item in items:
|
| 47 |
trending_projects.append({
|
|
|
|
| 59 |
print(f"Error fetching GitHub trending: {e}")
|
| 60 |
return cached_trending if cached_trending else []
|
| 61 |
|
| 62 |
+
# 定义MCP服务器和工具
|
| 63 |
+
mcp_server = FastMCP(name="GithubTrending", stateless_http=True)
|
| 64 |
|
| 65 |
@mcp_server.tool()
|
| 66 |
def get_trending_repos(num: int = 10) -> dict:
|
|
|
|
| 69 |
fetch_github_trending()
|
| 70 |
return {"trending": cached_trending[:num]}
|
| 71 |
|
| 72 |
+
mcp_app = mcp_server.http_app(path='/mcp')
|
| 73 |
+
app.mount("/mcp", mcp_app)
|
|
|
|
| 74 |
|
| 75 |
async def trending_generator():
|
| 76 |
+
"""SSE 事件生成器"""
|
| 77 |
while True:
|
| 78 |
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 79 |
await fetch_github_trending()
|
|
|
|
| 82 |
|
| 83 |
@app.get("/trending")
|
| 84 |
async def get_trending():
|
| 85 |
+
"""原有端点"""
|
| 86 |
if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 87 |
await fetch_github_trending()
|
| 88 |
return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
|
| 89 |
|
|
|
|
| 90 |
@app.get("/trending-sse")
|
| 91 |
async def get_trending_sse():
|
| 92 |
+
"""原有SSE端点"""
|
| 93 |
return StreamingResponse(
|
| 94 |
trending_generator(),
|
| 95 |
media_type="text/event-stream",
|