Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import requests
|
| 4 |
-
from fastapi import FastAPI,
|
| 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 |
|
|
@@ -61,7 +60,6 @@ async def fetch_github_trending():
|
|
| 61 |
|
| 62 |
# 定义MCP服务器和工具
|
| 63 |
mcp_server = FastMCP(name="GithubTrending")
|
| 64 |
-
app.mount("/mcp", mcp_server.http_app())
|
| 65 |
|
| 66 |
@mcp_server.tool()
|
| 67 |
def get_trending_repos(num: int = 10) -> dict:
|
|
@@ -70,15 +68,7 @@ def get_trending_repos(num: int = 10) -> dict:
|
|
| 70 |
fetch_github_trending()
|
| 71 |
return {"trending": cached_trending[:num]}
|
| 72 |
|
| 73 |
-
|
| 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()
|
| 80 |
-
yield f"data: {cached_trending}\n\n"
|
| 81 |
-
time.sleep(30)
|
| 82 |
|
| 83 |
@app.get("/trending")
|
| 84 |
async def get_trending():
|
|
@@ -87,11 +77,21 @@ async def get_trending():
|
|
| 87 |
await fetch_github_trending()
|
| 88 |
return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
|
| 89 |
|
| 90 |
-
@app.
|
| 91 |
-
async def
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import requests
|
| 4 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
from datetime import datetime, timedelta
|
|
|
|
| 7 |
from fastmcp.server import FastMCP
|
| 8 |
from contextlib import asynccontextmanager
|
| 9 |
|
|
|
|
| 60 |
|
| 61 |
# 定义MCP服务器和工具
|
| 62 |
mcp_server = FastMCP(name="GithubTrending")
|
|
|
|
| 63 |
|
| 64 |
@mcp_server.tool()
|
| 65 |
def get_trending_repos(num: int = 10) -> dict:
|
|
|
|
| 68 |
fetch_github_trending()
|
| 69 |
return {"trending": cached_trending[:num]}
|
| 70 |
|
| 71 |
+
app.mount("/mcp", mcp_server.http_app())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
@app.get("/trending")
|
| 74 |
async def get_trending():
|
|
|
|
| 77 |
await fetch_github_trending()
|
| 78 |
return {"trending": cached_trending, "last_updated": last_updated.isoformat() if last_updated else None}
|
| 79 |
|
| 80 |
+
@app.websocket("/trending-ws")
|
| 81 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 82 |
+
"""WebSocket 端点替代 SSE"""
|
| 83 |
+
await websocket.accept()
|
| 84 |
+
try:
|
| 85 |
+
while True:
|
| 86 |
+
if not last_updated or (datetime.now() - last_updated) > timedelta(minutes=5):
|
| 87 |
+
await fetch_github_trending()
|
| 88 |
+
await websocket.send_json({
|
| 89 |
+
"trending": cached_trending,
|
| 90 |
+
"last_updated": last_updated.isoformat() if last_updated else None
|
| 91 |
+
})
|
| 92 |
+
await asyncio.sleep(30) # 使用 asyncio.sleep 而不是 time.sleep
|
| 93 |
+
except WebSocketDisconnect:
|
| 94 |
+
print("Client disconnected")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"WebSocket error: {e}")
|
| 97 |
+
await websocket.close(code=1011)
|