Spaces:
Sleeping
Sleeping
File size: 2,058 Bytes
692e2cd 99fadc1 30f4f7f 99fadc1 378930b 3ccc294 bea46e9 860e1a7 bea46e9 692e2cd 378930b bea46e9 692e2cd 30f4f7f 99fadc1 30f4f7f 692e2cd 8db0f05 99fadc1 30f4f7f 99fadc1 30f4f7f 8db0f05 99fadc1 30f4f7f 8db0f05 378930b bea46e9 692e2cd 860e1a7 bea46e9 860e1a7 80dc3a4 692e2cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import contextlib
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pokemon.pokemon_server import mcp as pokemon_mcp
from geoguessr.geo_server import mcp as geogussr_mcp
from chess_game.chess_server import mcp as chess_mcp
from geoguessr.web_app import app as geoguessr_app
from chess_game.web_app import app as chess_app
import os
# Create a combined lifespan to manage both session managers
@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
async with contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(pokemon_mcp.session_manager.run())
await stack.enter_async_context(geogussr_mcp.session_manager.run())
await stack.enter_async_context(chess_mcp.session_manager.run())
yield
BASE_DIR = os.path.dirname(__file__)
STATIC_DIR = os.path.join(BASE_DIR, "static")
TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
app = FastAPI(lifespan=lifespan)
# Serve static assets (screenshot, styles)
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
templates = Jinja2Templates(directory=TEMPLATES_DIR)
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
space_host = os.environ.get("SPACE_HOST")
if space_host and space_host.strip():
base_url = space_host.strip().rstrip("/")
else:
base_url = f"{request.url.scheme}://{request.url.netloc}"
return templates.TemplateResponse("index.html", {"request": request, "base_url": base_url})
app.mount("/geoguessr", geogussr_mcp.streamable_http_app())
app.mount("/Pokemon", pokemon_mcp.streamable_http_app())
app.mount("/chess", chess_mcp.streamable_http_app())
# Mount GeoGuessr FastAPI web app (UI + API)
app.mount("/geoguessr_app", geoguessr_app)
app.mount("/chess_app", chess_app)
PORT = int(os.environ.get("PORT", "10000"))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=PORT)
|