feat: React control panel; pending-card review workflow; auth-method audit
Frontend rewrite (frontend/, replacing backend/static/): - Vite + TypeScript + React 19, React Router 7, zustand, TanStack Query 5 - dev: vite proxy /api -> :9000; prod: npm run build -> dist/, served by the backend with SPA fallback - pages: Control (gun bays w/ scope+readiness-gated start/stop, relays), History (who/via/gun/when/duration/kWh audit), Cards (pending review, enrolled, rejected), Users (permission checkbox matrix) - self-contained: fonts bundled, no CDN; light/dark; reduced-motion safe Backend: - learn mode removed: unknown cards are always refused and queued in pending_cards; admin approves (name + owner + gun restrictions in one step) or rejects (denied_cards, silent, un-rejectable); pending count rides /api/status as the notification signal - sessions record the auth method (card tap vs in-app button) for audit - GET /api/permissions: grantable-permission catalog driving the UI matrix - SPA static serving with path-traversal guard; 503 hint when not built Smoke test: 54 checks incl. approve/reject round-trips over a fake charge point and method audit assertions.
This commit is contained in:
+23
-17
@@ -17,13 +17,13 @@ from pathlib import Path
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, WebSocket
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import FileResponse, JSONResponse
|
||||
|
||||
from . import csms, routes
|
||||
from .config import CONFIG
|
||||
|
||||
STATIC = Path(__file__).resolve().parent / "static"
|
||||
# the built React app (frontend/: `npm run build` -> dist/)
|
||||
DIST = Path(__file__).resolve().parent.parent / "frontend" / "dist"
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
@@ -37,18 +37,23 @@ app = FastAPI(
|
||||
"ws://<host>:9000/<id>; everything else lives under /api/*.",
|
||||
)
|
||||
app.include_router(routes.router)
|
||||
app.mount("/static", StaticFiles(directory=STATIC), name="static")
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def index_page():
|
||||
return FileResponse(STATIC / "index.html")
|
||||
|
||||
|
||||
# the service worker must be served from / so its scope covers the whole app
|
||||
@app.get("/sw.js")
|
||||
async def service_worker():
|
||||
return FileResponse(STATIC / "sw.js", media_type="application/javascript")
|
||||
# SPA serving: exact files from frontend/dist, anything else falls back to
|
||||
# index.html so React Router owns the URL space. Registered after the API
|
||||
# router, so /api/* and /docs keep winning.
|
||||
@app.get("/{full_path:path}", include_in_schema=False)
|
||||
async def spa(full_path: str):
|
||||
if not DIST.exists():
|
||||
return JSONResponse(
|
||||
{"detail": "frontend not built — run `npm run build` in frontend/ "
|
||||
"(or use the Vite dev server, which proxies /api here)"},
|
||||
status_code=503,
|
||||
)
|
||||
candidate = (DIST / full_path).resolve() if full_path else None
|
||||
if candidate and candidate.is_file() and candidate.is_relative_to(DIST.resolve()):
|
||||
return FileResponse(candidate)
|
||||
return FileResponse(DIST / "index.html")
|
||||
|
||||
|
||||
# Catch-all WS route so the charger may dial any path (it uses /<ChargerID>).
|
||||
@@ -59,11 +64,12 @@ async def ocpp_websocket(ws: WebSocket, cp_path: str):
|
||||
|
||||
|
||||
async def main():
|
||||
mode = "LEARN (accepting & recording new ids)" if CONFIG["learn_mode"] else "ENFORCING allowlist"
|
||||
log.info(
|
||||
"CSMS ws://0.0.0.0:9000/<id> | API http://0.0.0.0:9000/docs | mode: %s | "
|
||||
"%d authorized id(s) | %d user(s) | %d relay(s) configured",
|
||||
mode, len(CONFIG["authorized"]), len(CONFIG["users"]), len(CONFIG["relays"]),
|
||||
"CSMS ws://0.0.0.0:9000/<id> | API http://0.0.0.0:9000/docs | "
|
||||
"%d authorized id(s) | %d pending card(s) | %d user(s) | %d relay(s) | panel %s",
|
||||
len(CONFIG["authorized"]), len(CONFIG["pending_cards"]),
|
||||
len(CONFIG["users"]), len(CONFIG["relays"]),
|
||||
"ready" if DIST.exists() else "NOT BUILT (frontend/: npm run build)",
|
||||
)
|
||||
if not CONFIG["users"]:
|
||||
log.warning(
|
||||
|
||||
Reference in New Issue
Block a user