Spaces:
Running
Running
Download app.py from RockCoder23/daawdawedrfewawe: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/spaces/RockCoder23/daawdawedrfewawe/resolve/main/app.py
- Command line
-
hf download hf://spaces/RockCoder23/daawdawedrfewawe/app.py
-
curl -L -o app.py https://huggingface.co/spaces/RockCoder23/daawdawedrfewawe/resolve/main/app.py
1.8 kB
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from typing import Dict, Any, Optional | |
| import time | |
| import asyncio | |
| app = FastAPI(title="Roblox Server Manager") | |
| servers: Dict[str, dict] = {} | |
| TIMEOUT = 60 | |
| lock = asyncio.Lock() | |
| class ServerInfo(BaseModel): | |
| ID: str | |
| PlaceId: int | |
| MaxPlayers: int | |
| CurrentPlayers: int | |
| Heartbeat: float | |
| ISPRIVATE: bool = False | |
| ActiveEvents: list[str] = [] | |
| class Config: | |
| extra = "ignore" | |
| def prune_servers(): | |
| now = time.time() | |
| expired = [ | |
| sid for sid, data in servers.items() | |
| if now - data.get("Heartbeat", 0) > TIMEOUT | |
| ] | |
| for sid in expired: | |
| del servers[sid] | |
| async def heartbeat(info: ServerInfo): | |
| async with lock: | |
| sid = info.ID | |
| if not sid: | |
| raise HTTPException(status_code=400, detail="Missing server ID") | |
| if sid not in servers: | |
| servers[sid] = {} | |
| # ✅ Safe update | |
| servers[sid].update({ | |
| "PlaceId": info.PlaceId, | |
| "MaxPlayers": info.MaxPlayers, | |
| "CurrentPlayers": info.CurrentPlayers, | |
| "ISPRIVATE": info.ISPRIVATE, | |
| "ActiveEvents": info.ActiveEvents or {}, | |
| "Heartbeat": time.time() | |
| }) | |
| prune_servers() | |
| return {"status":"ok","active_servers": servers} | |
| async def shutdown(info: ServerInfo): | |
| async with lock: | |
| if info.ID in servers: | |
| del servers[info.ID] | |
| return {"status": "ok"} | |
| return {"status": "not_found"} | |
| async def get_servers(): | |
| async with lock: | |
| prune_servers() | |
| return servers | |
| async def root(): | |
| return {"message": "Roblox Server Manager API is running!"} |