Spaces:
Running
Running
Update app.py
Browse files3 unmodified lines456789101112131415161718192019 unmodified lines40414243444511 unmodified lines57585960616263642 unmodified lines67686970717273747576772 unmodified lines808182838485868788899091923 unmodified linesfrom fastapi import FastAPI, Responsefrom fastapi.responses import StreamingResponsefrom datetime import datetime, timedeltafrom starlette.routing import Mountfrom fastmcp.server import FastMCPfrom contextlib import asynccontextmanager@asynccontextmanagerasync def lifespan(app: FastAPI):await fetch_github_trending()yieldapp = FastAPI(lifespan=lifespan)cached_trending = []last_updated = None19 unmodified linesresponse.raise_for_status()items = response.json().get("items", [])trending_projects = []for item in items:trending_projects.append({11 unmodified linesprint(f"Error fetching GitHub trending: {e}")return cached_trending if cached_trending else []#mcp_server = FastMCP(name="GithubTrending", stateless_http=True)mcp_server = FastMCP(name="GithubTrending")@mcp_server.tool()def get_trending_repos(num: int = 10) -> dict:2 unmodified linesfetch_github_trending()return {"trending": cached_trending[:num]}#app.mount("/mcp", mcp_server.streamable_http_app())app.mount("/mcp", mcp_server.http_app())#app.mount("/", mcp_server.http_app())async def trending_generator():while True:if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):await fetch_github_trending()2 unmodified lines@app.get("/trending")async def get_trending():if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):await fetch_github_trending()return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}@app.get("/trending-sse")async def get_trending_sse():return StreamingResponse(trending_generator(),media_type="text/event-stream",3 unmodified lines45678910111213141516171819202119 unmodified lines4142434445464711 unmodified lines59606162636465662 unmodified lines69707172737475767778792 unmodified lines82838485868788899091929394953 unmodified linesfrom fastapi import FastAPI, Responsefrom fastapi.responses import StreamingResponsefrom datetime import datetime, timedeltafrom starlette.routing import Mount # 用于挂载from fastmcp.server import FastMCPfrom contextlib import asynccontextmanager@asynccontextmanagerasync def lifespan(app: FastAPI):await fetch_github_trending() # 启动时初始化yieldapp = FastAPI(lifespan=lifespan)# 缓存最新获取的热门项目cached_trending = []last_updated = None19 unmodified linesresponse.raise_for_status()items = response.json().get("items", [])# 简化项目信息trending_projects = []for item in items:trending_projects.append({11 unmodified linesprint(f"Error fetching GitHub trending: {e}")return cached_trending if cached_trending else []# 定义MCP服务器和工具mcp_server = FastMCP(name="GithubTrending", stateless_http=True)@mcp_server.tool()def get_trending_repos(num: int = 10) -> dict:2 unmodified linesfetch_github_trending()return {"trending": cached_trending[:num]}mcp_app = mcp_server.http_app(path='/mcp')app.mount("/mcp", mcp_app)async def trending_generator():"""SSE 事件生成器"""while True:if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):await fetch_github_trending()2 unmodified lines@app.get("/trending")async def get_trending():"""原有端点"""if not cached_trending or (datetime.now() - last_updated) > timedelta(minutes=5):await fetch_github_trending()return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}@app.get("/trending-sse")async def get_trending_sse():"""原有SSE端点"""return StreamingResponse(trending_generator(),media_type="text/event-stream",
app.py+11-8