# punkystone/iobroker-history-api architecture

> Architecture diagram of the GitHub repository punkystone/iobroker-history-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/punkystone/iobroker-history-api
- Repository: https://github.com/punkystone/iobroker-history-api

## Overview

#### Purpose and Entry Point
This Go service exposes a narrow HTTP façade over ioBroker chart-history retrieval. The process starts in `cmd/api/main.go`, which loads environment-driven configuration, constructs the history client and HTTP server, then serves `POST /history`.

#### Runtime and Deployment
The application runs as a Go HTTP process and is intended to be containerized. `Dockerfile` defines the service image; `docker-compose.yml` supports local/container deployment. Runtime connection details come from environment variables via `internal/env/env.go`, notably the ioBroker socket.io endpoint, datasource instance, and debug mode.

#### HTTP API Boundary
`internal/server/server.go` owns HTTP routing and request/response orchestration for `/history`. It accepts JSON containing chart ID, time range, and requested value count; only POST is valid. HTTP-specific decoding, validation/error writing, and JSON response handling are separated into `internal/server/http_utils.go`.

#### History Integration
`internal/history/history.go` is the external-system adapter. It connects to the configured ioBroker socket.io service and issues the history lookup against the selected ioBroker datasource instance (for example, InfluxDB). It converts the remote response into timestamp/value history points returned by the API.

#### Primary Request Flow
1. `cmd/api/main.go` initializes configuration from `internal/env/env.go`.
2. It wires the ioBroker-backed history capability from `internal/history/history.go` into the HTTP server in `internal/server/server.go`.
3. A client POSTs `/history`; server code parses the JSON request and enforces method/body constraints.
4. The server delegates the lookup across the history adapter boundary to ioBroker socket.io.
5. The adapter returns points or an integration error; the server serializes either the success envelope or a `400`/`405` HTTP error.

#### Key Boundaries and Failure Semantics
The server layer isolates public HTTP behavior from ioBroker protocol details: callers do not connect to socket.io directly. Configuration isolates deployment-specific host and datasource selection. Invalid JSON and failed history lookups are surfaced as `400 Bad Request`; unsupported methods are `405 Method Not Allowed`. Debug logging of sent/received integration messages is controlled through environment configuration.

Main entry points: [`internal/server/server.go`](https://github.com/punkystone/iobroker-history-api/blob/main/internal/server/server.go) (History HTTP server), [`cmd/api/main.go`](https://github.com/punkystone/iobroker-history-api/blob/main/cmd/api/main.go) (API entry point), [`internal/history/history.go`](https://github.com/punkystone/iobroker-history-api/blob/main/internal/history/history.go) (ioBroker history client), [`Dockerfile`](https://github.com/punkystone/iobroker-history-api/blob/main/Dockerfile) (Service image)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Service Runtime"]
  node_dockerfile["Service image<br/>Docker build"]
  node_compose["Local deployment<br/>Compose<br/>[docker-compose.yml]"]
  node_main["API entry point<br/>Go bootstrap<br/>[main.go]"]
  node_env["Runtime configuration<br/>Environment loader<br/>[env.go]"]
end

subgraph group_api["HTTP API"]
  node_server["History HTTP server<br/>HTTP router<br/>[server.go]"]
  node_http_utils["HTTP JSON handling<br/>HTTP utilities<br/>[http_utils.go]"]
end

subgraph group_integration["History Integration"]
  node_history["ioBroker history client<br/>Socket.io adapter<br/>[history.go]"]
end

node_client(("API client<br/>HTTP consumer"))
node_iobroker{{"ioBroker socket.io<br/>External service"}}
node_datasource[("History datasource<br/>ioBroker instance")]

node_dockerfile -->|"packages"| node_main
node_compose -->|"runs image"| node_dockerfile
node_main -->|"loads"| node_env
node_main -->|"configures"| node_history
node_main -->|"wires"| node_server
node_env -->|"endpoint, datasource, debug"| node_history
node_client -->|"POST /history"| node_server
node_server -->|"decode and serialize"| node_http_utils
node_server -->|"history lookup"| node_history
node_history -->|"socket.io request"| node_iobroker
node_iobroker -->|"queries"| node_datasource
node_server -->|"points or 400/405"| node_client

click node_dockerfile "https://github.com/punkystone/iobroker-history-api/blob/main/Dockerfile"
click node_compose "https://github.com/punkystone/iobroker-history-api/blob/main/docker-compose.yml"
click node_main "https://github.com/punkystone/iobroker-history-api/blob/main/cmd/api/main.go"
click node_env "https://github.com/punkystone/iobroker-history-api/blob/main/internal/env/env.go"
click node_server "https://github.com/punkystone/iobroker-history-api/blob/main/internal/server/server.go"
click node_http_utils "https://github.com/punkystone/iobroker-history-api/blob/main/internal/server/http_utils.go"
click node_history "https://github.com/punkystone/iobroker-history-api/blob/main/internal/history/history.go"

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_dockerfile,node_compose,node_main,node_env toneBlue
class node_server,node_http_utils toneAmber
class node_history toneMint
class node_client,node_iobroker,node_datasource toneNeutral
```

## Components

### Service Runtime

- [Service image](https://github.com/punkystone/iobroker-history-api/blob/main/Dockerfile): Docker build · `Dockerfile`
- [Local deployment](https://github.com/punkystone/iobroker-history-api/blob/main/docker-compose.yml): Compose · `docker-compose.yml`
- [API entry point](https://github.com/punkystone/iobroker-history-api/blob/main/cmd/api/main.go): Go bootstrap · Builds configuration, history client, and HTTP server. · `cmd/api/main.go`
- [Runtime configuration](https://github.com/punkystone/iobroker-history-api/blob/main/internal/env/env.go): Environment loader · Reads socket.io endpoint, datasource instance, and debug mode. · `internal/env/env.go`

### HTTP API

- [History HTTP server](https://github.com/punkystone/iobroker-history-api/blob/main/internal/server/server.go): HTTP router · Owns POST /history orchestration. · `internal/server/server.go`
- [HTTP JSON handling](https://github.com/punkystone/iobroker-history-api/blob/main/internal/server/http_utils.go): HTTP utilities · Decodes requests and writes JSON errors and responses. · `internal/server/http_utils.go`

### History Integration

- [ioBroker history client](https://github.com/punkystone/iobroker-history-api/blob/main/internal/history/history.go): Socket.io adapter · Queries the selected ioBroker history datasource. · `internal/history/history.go`

### Other components

- **API client**: HTTP consumer
- **ioBroker socket.io**: External service
- **History datasource**: ioBroker instance · Selected datasource, such as InfluxDB.

## Connections

- Service image → API entry point: packages
- Local deployment → Service image: runs image
- API entry point → Runtime configuration: loads
- API entry point → ioBroker history client: configures
- API entry point → History HTTP server: wires
- Runtime configuration → ioBroker history client: endpoint, datasource, debug
- API client → History HTTP server: POST /history
- History HTTP server → HTTP JSON handling: decode and serialize
- History HTTP server → ioBroker history client: history lookup
- ioBroker history client → ioBroker socket.io: socket.io request
- ioBroker socket.io → History datasource: queries
- History HTTP server → API client: points or 400/405
