How thilac01/jpl-iot-middleware works
Purpose and Entry Points
JPL Security & IoT Middleware is a FastAPI integration layer between a Koha MySQL replica, RFID/Raspberry Pi security gates, and an operations dashboard. Primary application entry points are main.py (ASGI app, used by Gunicorn) and run.py (local execution). Static dashboard assets are served from static/index.html, static/app.js, and static/style.css.
Application Composition
The FastAPI lifecycle assembles configuration, logging, shared infrastructure, middleware, and API routers. Central application concerns are located in app/core/config.py, app/core/logging.py, and main.py. Runtime request boundaries add correlation IDs, error normalization, and security headers through app/middleware/correlation.py, app/middleware/error_handler.py, and app/middleware/security.py.
HTTP and WebSocket API Boundary
Routers separate externally visible capabilities:
• Circulation reads and Koha-oriented loan/audit views: app/routers/circulation.py.
• Safe database discovery/table inspection: app/routers/tables.py.
• IoT management and gate telemetry: app/routers/iot.py.
• Liveness, readiness, diagnostics, and metrics: app/routers/health.py.
• Persistent real-time client connections: app/routers/ws.py.
Clients enter through HTTP or /ws; routers coordinate services and return API/dashboard-facing data rather than exposing database or device access directly.
Database and Secure Connectivity
Database access is mediated by an asynchronous session/pool layer in app/db/session.py, with SSH-tunnel establishment in app/db/tunnel.py. The primary external data source is a remote Koha MySQL replica, reached via an SSH gateway and local tunnel binding. This boundary isolates credentials, reconnection behavior, and transport security from routers and services. Legacy/top-level database support may also exist in db_utils.py.
Change Data Capture and Real-Time Events
app/services/cdc_engine.py streams MySQL binlog mutations for Koha circulation-related tables, persists binlog position for restart recovery, and produces real-time events. The WebSocket layer in app/routers/ws.py consumes this event stream to push database mutations, checkout alerts, and gate signals to connected dashboards. CDC can trigger alert delivery through app/services/smtp_service.py.
IoT Gate Control Plane
app/services/iot_service.py owns Raspberry Pi gate discovery, registration, heartbeats, TTL/offline supervision, remote agent deployment, command execution, and device metrics. app/routers/iot.py is the controlled ingress for edge-node requests and administrative actions, keeping SSH/network operations behind the service boundary.
Koha Integration and Security
app/services/koha_service.py provides a dedicated integration point for Koha REST interactions, distinct from direct replica reads and CDC. Security policy is centralized in app/core/security.py and applied at request level by app/middleware/security.py; it includes optional API-key enforcement and protections around dynamic SQL/table access.
Deployment and Infrastructure
The service runs on Python/FastAPI, commonly behind Nginx (nginx.conf) for TLS, rate limiting, compression, and WebSocket upgrade handling. Deployment options include containers via Dockerfile, docker-compose.yml, and docker-compose.dev.yml; systemd via jpl-middleware.service; and multi-worker Gunicorn via gunicorn_conf.py. External operational dependencies are SSH, MySQL/Koha binlogs, SMTP, Raspberry Pi nodes, and an Nginx reverse proxy.
Components 21
Edge & clients
- Nginx proxy · TLS, rate limits, compression, and WebSocket upgrades.
nginx.conf - Operations dashboard
static/index.html
FastAPI application
- FastAPI app · Assembles lifecycle infrastructure, middleware, routers, and static assets.
main.py - Config & logging
app/core/config.py - Request middleware · Adds correlation IDs, normalized errors, and security headers.
app/middleware/correlation.py - Security policy · API-key and dynamic SQL/table-access protections.
app/core/security.py - Circulation API
app/routers/circulation.py - Table discovery API
app/routers/tables.py - IoT management API
app/routers/iot.py - Health & metrics API
app/routers/health.py - WebSocket hub · Pushes mutation, checkout-alert, and gate events to dashboards.
app/routers/ws.py
Services & data
- Async DB sessions
app/db/session.py - SSH tunnel · Establishes the local binding to the remote replica.
app/db/tunnel.py - CDC engine · Streams circulation mutations and persists restart position.
app/services/cdc_engine.py - Gate control service · Manages Pi discovery, heartbeats, deployment, commands, and metrics.
app/services/iot_service.py - Koha REST service
app/services/koha_service.py - SMTP alerts
app/services/smtp_service.py
External systems
- Koha MySQL replica · Provides direct reads and binlog mutations.
- SSH gateway
- Raspberry Pi gates
Deployment
- Gunicorn / containers / systemd
gunicorn_conf.py
Connections 23
- Operations dashboard → Nginx proxy: HTTP and /ws
- Nginx proxy → FastAPI app: proxies requests
- FastAPI app → Operations dashboard: serves assets
- FastAPI app → Config & logging: initializes
- FastAPI app → Request middleware: installs
- Request middleware → Security policy: enforces
- FastAPI app → Circulation API: mounts
- FastAPI app → Table discovery API: mounts
- FastAPI app → IoT management API: mounts
- FastAPI app → Health & metrics API: mounts
- FastAPI app → WebSocket hub: mounts
- Circulation API → Async DB sessions: replica reads
- Circulation API → Koha REST service: Koha REST
- Table discovery API → Async DB sessions: safe inspection
- IoT management API → Gate control service: controls
- Gate control service → Raspberry Pi gates: manages
- Async DB sessions → SSH tunnel: connects through
- SSH tunnel → SSH gateway: establishes
- SSH gateway → Koha MySQL replica: tunnels to
- CDC engine → Koha MySQL replica: reads binlog
- CDC engine → WebSocket hub: real-time events
- CDC engine → SMTP alerts: alerts
- Gunicorn / containers / systemd → FastAPI app: runs ASGI app