Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,13 +13,14 @@ async def lifespan(app: FastAPI):
|
|
| 13 |
|
| 14 |
app = FastAPI(lifespan=lifespan)
|
| 15 |
|
| 16 |
-
#
|
| 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]}
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
app.mount("/mcp", mcp_server.http_app(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
| 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
|