diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-08-09 04:39:34 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-08-09 04:39:34 +0200 |
| commit | fb91c4545c757711e1b5fd354ca4b311c89fd2c0 (patch) | |
| tree | eb1aee6cc0fb5fc020eed2763009dea5a32eb8ee /packages/meshbay-hub/src/meshbay_hub/api/nodes.py | |
| parent | 77d76421829161df6b1ef628b4e6e051a2c3c2ee (diff) | |
| download | meshbay-fb91c4545c757711e1b5fd354ca4b311c89fd2c0.tar.gz | |
feat(hub): add production hub — config, auth, API routers, tests
config.py: TOML + env var priority. auth.py: Argon2id passwords,
JWT EdDSA with jti, refresh token hashed (blake3). Routers:
hub (info/pubkey), users (register/login/refresh/pubkeys),
nodes (announce/get), groups (create/gek-bundle/gek-retrieve).
Rate limiting via slowapi. app.py factory with lifespan.
All 40 tests pass (SQLite in-memory, no PostgreSQL required).
Fix: remove tests/__init__.py to resolve namespace conflicts.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/api/nodes.py')
| -rw-r--r-- | packages/meshbay-hub/src/meshbay_hub/api/nodes.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/api/nodes.py b/packages/meshbay-hub/src/meshbay_hub/api/nodes.py new file mode 100644 index 0000000..b970aa8 --- /dev/null +++ b/packages/meshbay-hub/src/meshbay_hub/api/nodes.py @@ -0,0 +1,66 @@ +"""Node endpoints — /v1/nodes/*""" + +from fastapi import APIRouter, Depends, HTTPException, Request +from pydantic import BaseModel +from sqlalchemy.ext.asyncio import AsyncSession + +from meshbay_hub.api.deps import get_current_user +from meshbay_hub.db.engine import get_db +from meshbay_hub.db.models import IPLog, Node, User + +router = APIRouter(prefix="/v1/nodes", tags=["nodes"]) + + +class NodeAnnounceRequest(BaseModel): + pk_node: str + endpoint_hint: str | None = None + + +@router.post("/announce", status_code=201) +async def announce_node( + body: NodeAnnounceRequest, + request: Request, + current_user: User = Depends(get_current_user), + db: AsyncSession = Depends(get_db), +): + node = Node( + user_id=current_user.id, + pk_node=body.pk_node, + endpoint_hint=body.endpoint_hint, + ) + db.add(node) + db.add(IPLog( + user_id=current_user.id, + event="node_announce", + ip_address=_ip(request), + detail=body.endpoint_hint, + )) + await db.commit() + await db.refresh(node) + return {"node_id": node.id} + + +@router.get("/{node_id}") +async def get_node( + node_id: str, + current_user: User = Depends(get_current_user), + db: AsyncSession = Depends(get_db), +): + node = await db.get(Node, node_id) + if not node: + raise HTTPException(status_code=404, detail="Node not found") + owner = await db.get(User, node.user_id) + return { + "node_id": node.id, + "username": owner.username if owner else "", + "pk_node": node.pk_node, + "endpoint_hint": node.endpoint_hint, + "announced_at": node.announced_at.isoformat(), + } + + +def _ip(request: Request) -> str: + fwd = request.headers.get("X-Forwarded-For") + if fwd: + return fwd.split(",")[0].strip() + return request.client.host if request.client else "unknown" |