test: add docker-compose with mosquitto and python subscriber
This commit is contained in:
23
test/README.md
Normal file
23
test/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Test compose
|
||||
|
||||
This folder contains a simple Docker Compose stack to run the Go server together with an MQTT broker and a Python subscriber client.
|
||||
|
||||
Quickstart:
|
||||
|
||||
```bash
|
||||
cd test
|
||||
docker compose up --build
|
||||
```
|
||||
|
||||
- The Go server is built from the repository `Dockerfile` and is configured via environment variables in `docker-compose.yml`.
|
||||
- The MQTT broker is `eclipse-mosquitto` and is exposed on port `1883`.
|
||||
- The `subscriber` service runs `subscribe.py` and prints any messages published to the `lambdaiot` topic.
|
||||
|
||||
To run the Python subscriber locally instead of in Docker:
|
||||
|
||||
```bash
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
MQTT_BROKER=localhost:1883 MQTT_TOPIC=lambdaiot python subscribe.py
|
||||
```
|
||||
42
test/docker-compose.yml
Normal file
42
test/docker-compose.yml
Normal file
@@ -0,0 +1,42 @@
|
||||
version: "3.8"
|
||||
|
||||
services:
|
||||
server:
|
||||
build:
|
||||
context: ..
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "8080:8080"
|
||||
environment:
|
||||
- SERVER_ADDRESS=0.0.0.0
|
||||
- SERVER_PORT=8080
|
||||
- JWT_SECRET=secret
|
||||
- MQTT_BROKER=tcp://mosquitto:1883
|
||||
- MQTT_CLIENT_ID=lambdaiot-server
|
||||
- MQTT_TOPIC=lambdaiot
|
||||
depends_on:
|
||||
- mosquitto
|
||||
|
||||
mosquitto:
|
||||
image: eclipse-mosquitto:2.0
|
||||
ports:
|
||||
- "1883:1883"
|
||||
volumes:
|
||||
- mosquitto_data:/mosquitto/data
|
||||
- mosquitto_log:/mosquitto/log
|
||||
|
||||
subscriber:
|
||||
image: python:3.11-slim
|
||||
volumes:
|
||||
- ./subscribe.py:/app/subscribe.py
|
||||
working_dir: /app
|
||||
environment:
|
||||
- MQTT_BROKER=mosquitto:1883
|
||||
- MQTT_TOPIC=lambdaiot
|
||||
command: sh -c "pip install paho-mqtt && python subscribe.py"
|
||||
depends_on:
|
||||
- mosquitto
|
||||
|
||||
volumes:
|
||||
mosquitto_data:
|
||||
mosquitto_log:
|
||||
1
test/requirements.txt
Normal file
1
test/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
paho-mqtt==1.6.1
|
||||
38
test/subscribe.py
Normal file
38
test/subscribe.py
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
broker = os.getenv('MQTT_BROKER', 'localhost:1883')
|
||||
topic = os.getenv('MQTT_TOPIC', 'lambdaiot')
|
||||
|
||||
if broker.startswith('tcp://'):
|
||||
broker = broker[len('tcp://'):]
|
||||
|
||||
host, sep, port = broker.partition(':')
|
||||
port = int(port) if sep else 1883
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print(f"Connected with result code {rc}")
|
||||
client.subscribe(topic)
|
||||
print(f"Subscribed to {topic}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
try:
|
||||
payload = msg.payload.decode()
|
||||
except Exception:
|
||||
payload = str(msg.payload)
|
||||
print(f"[{msg.topic}] {payload}")
|
||||
|
||||
client = mqtt.Client()
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
||||
try:
|
||||
client.connect(host, port, 60)
|
||||
except Exception as e:
|
||||
print("Could not connect to broker:", e)
|
||||
sys.exit(1)
|
||||
|
||||
client.loop_forever()
|
||||
Reference in New Issue
Block a user