feat: enhance Dockerfile for SQLite support and update main.go for MySQL connection; add sensor creation endpoint and improve wait-for-mqtt script

This commit is contained in:
2026-01-10 14:20:38 +01:00
parent a6196cc0ee
commit 81677943c4
5 changed files with 89 additions and 35 deletions
+29 -10
View File
@@ -6,12 +6,12 @@ Repository: https://git.piskot.si/SeminarM2/lambdaiot-core
## Features ✅
- Simple HTTP server with two endpoints:
- `GET /health` — basic health check
- `GET /hello` — example greeting endpoint
- Tests for handlers using `httptest`
- Multi-stage `Dockerfile` for small production images
- `Makefile` with common tasks (build, run, test, docker-build)
- HTTP API with health, greeting, auth, device CRUD, sensor creation, MQTT ping, and stored message retrieval
- JWT-based auth middleware with demo login (`admin`/`password`)
- MQTT client with startup publish and best-effort subscription to persist `state:` topics into SQLite
- SQLite sidecar (file) for recent MQTT state messages
- Multi-stage `Dockerfile` (CGO-enabled for sqlite3) and `Makefile` for common tasks
- `test/docker-compose.yml` spins up MySQL, Mosquitto, phpMyAdmin, and the server for local integration
---
@@ -42,16 +42,35 @@ Then run it:
docker run -p 8080:8080 lambdaiot-core:latest
```
Integration stack (MySQL + Mosquitto + server):
```bash
cd test
docker compose up -d --build
```
The compose file seeds the database from `ai-improved.sql` and exposes:
- API on http://localhost:8080
- MySQL on localhost:3306 (root/rootpass)
- Mosquitto on localhost:1883
- phpMyAdmin on http://localhost:8081
### Endpoints
- `http://localhost:8080/health` — returns `{ "status": "ok" }`
- `http://localhost:8080/hello` — returns a small greeting JSON
- `POST http://localhost:8080/login` — demo login, JSON body: `{ "username": "admin", "password": "password" }`, returns `{ "token": "..." }`
- `GET http://localhost:8080/protected` — protected endpoint requiring `Authorization: Bearer <token>` header
- `GET /health` — returns `{ "status": "ok" }`
- `GET /hello` greeting JSON
- `POST /login` — demo login, body `{ "username": "admin", "password": "password" }`, returns `{ "token": "..." }`
- `GET /protected` — JWT required
- `GET /devices` — list devices (public) / `POST /devices`, `GET/PUT/DELETE /devices/:id` (JWT)
- `POST /sensors` — create sensor (JWT)
- `GET /mqttping` — publish timestamp to MQTT default topic (JWT)
- `GET /messages` — last stored MQTT state messages from SQLite (JWT)
### Environment
- `JWT_SECRET` — (optional) secret used to sign tokens; defaults to `secret` for local/dev use
- `MQTT_*` — configure broker, client ID, topic, username/password
- `DB_*` — MySQL connection overrides; see `config.toml` defaults in `internal/config`
---