-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscovery-server.py
More file actions
29 lines (25 loc) · 813 Bytes
/
discovery-server.py
File metadata and controls
29 lines (25 loc) · 813 Bytes
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
from fastapi import FastAPI, Request
from datetime import datetime, timedelta
import uvicorn
app = FastAPI(title="Agent Discovery Service")
registry = {}
@app.post("/register")
async def register(request: Request):
data = await request.json()
name = data["name"]
registry[name] = {
"url": data["url"],
"last_heartbeat": datetime.utcnow()
}
return {"status": "registered", "agent": name}
@app.get("/services")
async def list_services():
now = datetime.utcnow()
alive = {
n: {"url": d["url"], "last_heartbeat": d["last_heartbeat"].isoformat()}
for n, d in registry.items()
if now - d["last_heartbeat"] < timedelta(seconds=30)
}
return {"active_agents": alive}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8500)