From fb726f7879a441208a7c3aeff5d07d8b9202bff8 Mon Sep 17 00:00:00 2001 From: Ammonite Date: Sun, 5 Oct 2025 19:38:00 +0200 Subject: [PATCH 1/6] Remove old test file --- test.py | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index 7ca7523..0000000 --- a/test.py +++ /dev/null @@ -1,32 +0,0 @@ -import json -import paho.mqtt.client as mqtt - -# The callback for when the client receives a CONNACK response from the server. -def on_connect(client, userdata, flags, rc): - print("Connected with result code "+str(rc)) - - # Subscribing in on_connect() means that if we lose the connection and - # reconnect then subscriptions will be renewed. - client.subscribe("$SYS/#") - -# The callback for when a PUBLISH message is received from the server. -def on_message(client, userdata, msg): - print(msg.topic+" "+str(msg.payload)) - - if msg.topic == "$SYS/broker/uptime": - uptime = int(msg.payload) - if uptime % 2 == 0: - client.publish("mqtt2http", json.dumps({"uptime": uptime})) - -client = mqtt.Client() -client.on_connect = on_connect -client.on_message = on_message -client.username_pw_set("test", "test") - -client.connect("localhost", 1883, 60) - -# Blocking call that processes network traffic, dispatches callbacks and -# handles reconnecting. -# Other loop*() functions are available that give a threaded interface and a -# manual interface. -client.loop_forever() From 1965ea8ee9c6fd84f04199b48e1c74244c2150f6 Mon Sep 17 00:00:00 2001 From: Ammonite Date: Sun, 5 Oct 2025 19:38:38 +0200 Subject: [PATCH 2/6] Add documentation for the route file --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index ad499db..230599f 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ A simple MQTT broker that connects to your HTTP services for login and message h * [Quick Start](#quick-start) * [Docker](#docker) * [Configuration](#configuration) +* [Routing](#routing) * [Metrics](#metrics) ## Features @@ -80,6 +81,32 @@ image: docker.io/amm0nite/mqtt2http:1.0.0 | `MQTT2HTTP_ROUTES_FILE_PATH` | `routes.yaml` | Path for the yaml file that defines all routes. | `MQTT2HTTP_API_PASSWORD` | random value | Password used to secure the API endpoints. +## Routing + +Define fine-grained routing rules in a YAML file that is loaded at start-up. By default the broker looks for `routes.yaml` in the working directory, or you can set `MQTT2HTTP_ROUTES_FILE_PATH` to point to a different file. + +Each entry in the file is a map with three fields: + +* `name`: friendly identifier used in logs when the route matches. +* `pattern`: Go regular expression tested against the MQTT topic (`^` / `$` anchors are optional). +* `url`: target HTTP endpoint to receive the forwarded payload. Leave empty to drop messages for this route after a match. + +Example `routes.yaml`: + +```yaml +- name: telemetry + pattern: '^sensors/.+' + url: https://example.com/iot/publish +- name: drop-debug + pattern: '^debug/' + url: '' +- name: fallback + pattern: '.*' + url: https://example.com/default/{topic} +``` + +Routes are evaluated in order and the first match wins. If no route matches, the broker logs the miss and no HTTP request is sent. When the routes file is empty (or missing) and `MQTT2HTTP_PUBLISH_URL` is configured, a default catch-all route using that URL is created automatically. + ## Metrics Prometheus metrics are available at `/metrics` on the configured metrics address (`MQTT2HTTP_METRICS_HTTP_LISTEN_ADDRESS`). From 9517a0dd8c9054bb87ca69352fabde84e417a050 Mon Sep 17 00:00:00 2001 From: Ammonite Date: Sun, 5 Oct 2025 19:40:55 +0200 Subject: [PATCH 3/6] Add doc on publish API auth --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 230599f..2930719 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Set the URLs for your HTTP services using environment variables: ```env MQTT2HTTP_AUTHORIZE_URL=http://... MQTT2HTTP_PUBLISH_URL=http://... +MQTT2HTTP_API_PASSWORD=somesecret ``` ### MQTT to HTTP @@ -39,10 +40,10 @@ MQTT2HTTP_PUBLISH_URL=http://... ### HTTP to MQTT -* Publish messages to MQTT topics using the built-in REST API. +* Publish messages to MQTT topics using the built-in REST API. Requests must include HTTP Basic Auth with the password set in `MQTT2HTTP_API_PASSWORD` (the username is ignored). The default password is random at start-up, so set it explicitly if you want to call the API. ```bash -curl --user username:password -X POST -d '{"test": true}' http://mqtt2http:8080/publish?topic=hello +curl --user user:somesecret -X POST -d '{"test": true}' http://mqtt2http:8080/publish?topic=hello ``` ## Docker From d40040e025e34c10e3da7734582fd60422e50d9c Mon Sep 17 00:00:00 2001 From: Ammonite Date: Sun, 5 Oct 2025 19:48:41 +0200 Subject: [PATCH 4/6] Change short description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2930719..4047d41 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MQTT2HTTP -A simple MQTT broker that connects to your HTTP services for login and message handling. +MQTT broker that forwards topics to HTTP endpoints with configurable routing. ## Table of Contents From d98e359bdf6b759ab6991a9af20ca94f8679e17e Mon Sep 17 00:00:00 2001 From: Ammonite Date: Sun, 5 Oct 2025 19:56:07 +0200 Subject: [PATCH 5/6] Add info on the catch all URL --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4047d41..0d0f75f 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ image: docker.io/amm0nite/mqtt2http:1.0.0 | `MQTT2HTTP_MQTT_LISTEN_ADDRESS` | `:1883` | Address where the MQTT broker listens (host\:port). | | `MQTT2HTTP_HTTP_LISTEN_ADDRESS` | `:8080` | Address for the HTTP REST API (`/publish` endpoint). | | `MQTT2HTTP_AUTHORIZE_URL` | `http://example.com` | HTTP Basic Auth endpoint for authorizing `CONNECT` requests. A 200/201 response allows access. | -| `MQTT2HTTP_PUBLISH_URL` | `http://example.com/{topic}` | Template URL for forwarding `PUBLISH` messages. `{topic}` is replaced dynamically. | +| `MQTT2HTTP_PUBLISH_URL` | `http://example.com/{topic}` | Template URL for forwarding `PUBLISH` messages; `{topic}` is replaced dynamically. When no routes file is loaded, this URL is used for a catch-all default route. | | `MQTT2HTTP_CONTENT_TYPE` | `application/octet-stream` | `Content-Type` header used in forwarded HTTP `POST` requests. E.g., `application/json`. | | `MQTT2HTTP_TOPIC_HEADER` | `X-Topic` | Name of the HTTP header that carries the MQTT topic. | | `MQTT2HTTP_METRICS_HTTP_LISTEN_ADDRESS` | `:9090` | Address for serving Prometheus metrics at the `/metrics` endpoint. | From c34a7086814ae714ee21d1bb558f05947edb5ba3 Mon Sep 17 00:00:00 2001 From: Ammonite Date: Sun, 5 Oct 2025 20:42:05 +0200 Subject: [PATCH 6/6] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d0f75f..612d928 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ MQTT broker that forwards topics to HTTP endpoints with configurable routing. ## Missing features -* **ACL**: Limits wich topic can be published or subscribed to. Everything is allowed for now. +* **ACL**: Limits which topic can be published or subscribed to. Everything is allowed for now. ## Quick Start