# thilac01/jpl-iot-middleware architecture

> Architecture diagram of the GitHub repository thilac01/jpl-iot-middleware, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thilac01/jpl-iot-middleware
- Repository: https://github.com/thilac01/jpl-iot-middleware

## Overview

#### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Edge &amp; clients"]
  node_nginx{{"Nginx proxy<br/>reverse proxy<br/>[nginx.conf]"}}
  node_dashboard["Operations dashboard<br/>static client<br/>[index.html]"]
end

subgraph group_api["FastAPI application"]
  node_main{{"FastAPI app<br/>ASGI composition<br/>[main.py]"}}
  node_config["Config &amp; logging<br/>core runtime<br/>[config.py]"]
  node_middleware["Request middleware<br/>request boundary<br/>[correlation.py]"]
  node_security["Security policy<br/>access policy<br/>[security.py]"]
  node_circulation["Circulation API<br/>HTTP router<br/>[circulation.py]"]
  node_tables["Table discovery API<br/>HTTP router<br/>[tables.py]"]
  node_iot_api["IoT management API<br/>HTTP router<br/>[iot.py]"]
  node_health["Health &amp; metrics API<br/>HTTP router<br/>[health.py]"]
  node_ws["WebSocket hub<br/>WebSocket router<br/>[ws.py]"]
end

subgraph group_services["Services &amp; data"]
  node_db_session[("Async DB sessions<br/>database access<br/>[session.py]")]
  node_ssh_tunnel["SSH tunnel<br/>secure transport<br/>[tunnel.py]"]
  node_cdc["CDC engine<br/>binlog pipeline<br/>[cdc_engine.py]"]
  node_iot_service["Gate control service<br/>device control plane<br/>[iot_service.py]"]
  node_koha_service["Koha REST service<br/>Koha integration<br/>[koha_service.py]"]
  node_smtp["SMTP alerts<br/>alert delivery<br/>[smtp_service.py]"]
end

subgraph group_external["External systems"]
  node_koha_mysql[("Koha MySQL replica<br/>remote database")]
  node_ssh_gateway{{"SSH gateway<br/>remote gateway"}}
  node_pi_nodes["Raspberry Pi gates<br/>edge devices"]
end

subgraph group_deploy["Deployment"]
  node_runtime["Gunicorn / containers / systemd<br/>runtime deployment<br/>[gunicorn_conf.py]"]
end

node_dashboard -->|"HTTP and /ws"| node_nginx
node_nginx -->|"proxies requests"| node_main
node_main -->|"serves assets"| node_dashboard
node_main -->|"initializes"| node_config
node_main -->|"installs"| node_middleware
node_middleware -->|"enforces"| node_security
node_main -->|"mounts"| node_circulation
node_main -->|"mounts"| node_tables
node_main -->|"mounts"| node_iot_api
node_main -->|"mounts"| node_health
node_main -->|"mounts"| node_ws
node_circulation -->|"replica reads"| node_db_session
node_circulation -->|"Koha REST"| node_koha_service
node_tables -->|"safe inspection"| node_db_session
node_iot_api -->|"controls"| node_iot_service
node_iot_service -->|"manages"| node_pi_nodes
node_db_session -->|"connects through"| node_ssh_tunnel
node_ssh_tunnel -->|"establishes"| node_ssh_gateway
node_ssh_gateway -->|"tunnels to"| node_koha_mysql
node_cdc -->|"reads binlog"| node_koha_mysql
node_cdc -->|"real-time events"| node_ws
node_cdc -->|"alerts"| node_smtp
node_runtime -->|"runs ASGI app"| node_main

click node_nginx "https://github.com/thilac01/jpl-iot-middleware/blob/main/nginx.conf"
click node_dashboard "https://github.com/thilac01/jpl-iot-middleware/blob/main/static/index.html"
click node_main "https://github.com/thilac01/jpl-iot-middleware/blob/main/main.py"
click node_config "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/core/config.py"
click node_middleware "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/middleware/correlation.py"
click node_security "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/core/security.py"
click node_circulation "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/circulation.py"
click node_tables "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/tables.py"
click node_iot_api "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/iot.py"
click node_health "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/health.py"
click node_ws "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/ws.py"
click node_db_session "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/db/session.py"
click node_ssh_tunnel "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/db/tunnel.py"
click node_cdc "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/cdc_engine.py"
click node_iot_service "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/iot_service.py"
click node_koha_service "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/koha_service.py"
click node_smtp "https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/smtp_service.py"
click node_runtime "https://github.com/thilac01/jpl-iot-middleware/blob/main/gunicorn_conf.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_nginx,node_dashboard toneBlue
class node_main,node_config,node_middleware,node_security,node_circulation,node_tables,node_iot_api,node_health,node_ws toneAmber
class node_db_session,node_ssh_tunnel,node_cdc,node_iot_service,node_koha_service,node_smtp toneMint
class node_koha_mysql,node_ssh_gateway,node_pi_nodes toneRose
class node_runtime toneIndigo
```

## Components

### Edge & clients

- [Nginx proxy](https://github.com/thilac01/jpl-iot-middleware/blob/main/nginx.conf): reverse proxy · TLS, rate limits, compression, and WebSocket upgrades. · `nginx.conf`
- [Operations dashboard](https://github.com/thilac01/jpl-iot-middleware/blob/main/static/index.html): static client · `static/index.html`

### FastAPI application

- [FastAPI app](https://github.com/thilac01/jpl-iot-middleware/blob/main/main.py): ASGI composition · Assembles lifecycle infrastructure, middleware, routers, and static assets. · `main.py`
- [Config & logging](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/core/config.py): core runtime · `app/core/config.py`
- [Request middleware](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/middleware/correlation.py): request boundary · Adds correlation IDs, normalized errors, and security headers. · `app/middleware/correlation.py`
- [Security policy](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/core/security.py): access policy · API-key and dynamic SQL/table-access protections. · `app/core/security.py`
- [Circulation API](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/circulation.py): HTTP router · `app/routers/circulation.py`
- [Table discovery API](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/tables.py): HTTP router · `app/routers/tables.py`
- [IoT management API](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/iot.py): HTTP router · `app/routers/iot.py`
- [Health & metrics API](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/health.py): HTTP router · `app/routers/health.py`
- [WebSocket hub](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/routers/ws.py): WebSocket router · Pushes mutation, checkout-alert, and gate events to dashboards. · `app/routers/ws.py`

### Services & data

- [Async DB sessions](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/db/session.py): database access · `app/db/session.py`
- [SSH tunnel](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/db/tunnel.py): secure transport · Establishes the local binding to the remote replica. · `app/db/tunnel.py`
- [CDC engine](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/cdc_engine.py): binlog pipeline · Streams circulation mutations and persists restart position. · `app/services/cdc_engine.py`
- [Gate control service](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/iot_service.py): device control plane · Manages Pi discovery, heartbeats, deployment, commands, and metrics. · `app/services/iot_service.py`
- [Koha REST service](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/koha_service.py): Koha integration · `app/services/koha_service.py`
- [SMTP alerts](https://github.com/thilac01/jpl-iot-middleware/blob/main/app/services/smtp_service.py): alert delivery · `app/services/smtp_service.py`

### External systems

- **Koha MySQL replica**: remote database · Provides direct reads and binlog mutations.
- **SSH gateway**: remote gateway
- **Raspberry Pi gates**: edge devices

### Deployment

- [Gunicorn / containers / systemd](https://github.com/thilac01/jpl-iot-middleware/blob/main/gunicorn_conf.py): runtime deployment · `gunicorn_conf.py`

## Connections

- 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
