Phase 4: cloud backend — licensing, heartbeat, site management
- New cloud_backend/ FastAPI service on port 8001 (SQLite for dev, swappable to PostgreSQL) - Endpoints: sysadmin auth (JWT), site registration, lock/unlock, heartbeat (X-Site-ID + X-Site-Key headers) - Default sysadmin seeded on first startup from ADMIN_USERNAME/ADMIN_PASSWORD env vars - cloud_backend added to docker-compose.yml with persistent data volume at ./data/cloud/ - local_backend cloud_sync.py updated to use correct /api/heartbeat/ endpoint with header auth - local_backend config.py: added SITE_KEY setting - Smoke tested: login, register site, heartbeat, lock, unlock, list all pass
This commit is contained in:
37
cloud_backend/routers/heartbeat.py
Normal file
37
cloud_backend/routers/heartbeat.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import APIRouter, Depends, HTTPException, Header, Request, status
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from database import get_db
|
||||
from models.site import Site
|
||||
from schemas.site import HeartbeatRequest, HeartbeatResponse
|
||||
|
||||
router = APIRouter()
|
||||
_pwd = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
@router.post("/", response_model=HeartbeatResponse)
|
||||
def heartbeat(
|
||||
body: HeartbeatRequest,
|
||||
request: Request,
|
||||
x_site_id: str = Header(..., alias="X-Site-ID"),
|
||||
x_site_key: str = Header(..., alias="X-Site-Key"),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
site = db.query(Site).filter(Site.site_id == x_site_id).first()
|
||||
if not site or not _pwd.verify(x_site_key, site.secret_key_hash):
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid site credentials")
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
site.last_seen_at = now
|
||||
site.last_seen_ip = request.client.host if request.client else None
|
||||
db.commit()
|
||||
|
||||
licensed = site.is_active and (site.license_expires_at.replace(tzinfo=timezone.utc) > now)
|
||||
return HeartbeatResponse(
|
||||
licensed=licensed,
|
||||
locked=site.is_locked,
|
||||
lock_reason=site.lock_reason,
|
||||
expires_at=site.license_expires_at,
|
||||
)
|
||||
Reference in New Issue
Block a user