A comprehensive backend server for managing MQTT servers and monitoring system health. This application provides REST APIs, a web-based dashboard, and automated health monitoring with persistent storage.
- MQTT Server Management: Start, stop, and restart Mosquitto MQTT broker via REST API
- Screen Session Integration: MQTT server runs in detached screen sessions
- System Health Monitoring: Real-time CPU, memory, and disk usage tracking
- Resource Usage Tracking: Monitor MQTT server's CPU and memory consumption
- Persistent Storage: Dual storage support (JSON files + PostgreSQL)
- Automated Status Saving: Status persisted every 15 seconds
- Web Dashboard: Real-time monitoring UI with auto-refresh
- REST API: Complete API with Swagger documentation
- Type Safety: Full type hints throughout the codebase
- Modular Architecture: OOP design with clean separation of concerns
- Comprehensive Tests: Unit tests for all major components
backend/
├── src/
│ ├── config/ # Configuration management
│ ├── models/ # Pydantic data models
│ ├── persistence/ # Storage layer (JSON + PostgreSQL)
│ ├── services/ # Business logic (MQTT, health monitoring)
│ ├── routes/ # REST API endpoints
│ ├── static/ # Frontend assets (CSS, JS)
│ └── templates/ # HTML templates
├── tests/ # Comprehensive test suite
├── data/ # JSON file storage
├── config.yaml # Application configuration
└── main.py # Application entry point
- Python 3.9+
- PostgreSQL (optional, falls back to JSON)
- Mosquitto MQTT broker
- Screen command-line utility
-
Clone the repository:
cd backend -
Create virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Configure the application: Edit
config.yamlto customize:- MQTT broker command
- Server host and port
- Database credentials
- Monitoring intervals
The config.yaml file controls all aspects of the application:
mqtt:
command: "mosquitto -c /etc/mosquitto/mosquitto.conf"
screen_session_name: "mqtt_server"
port: 1883
host: "localhost"
server:
host: "0.0.0.0"
port: 8080
debug: true
persistence:
json_file: "data/mqtt_status.json"
postgres:
host: "localhost"
port: 5432
database: "mqtt_manager"
user: "postgres"
password: "postgres"
monitoring:
status_save_interval: 15 # seconds
system:
cpu_threshold: 80
memory_threshold: 80The easiest way to run the application is using Docker Compose:
-
Build and start all services:
docker-compose up -d
-
View logs:
docker-compose logs -f mqtt_manager
-
Stop services:
docker-compose down
-
Using Makefile shortcuts:
make up # Start all services make logs # View logs make down # Stop all services make restart # Restart services make clean # Remove containers and volumes make test # Run tests in container make shell # Open shell in container
-
Start the server:
python main.py
-
Access the dashboard:
- Dashboard: http://localhost:8080/
- API Documentation: http://localhost:8080/api/docs
- Health Check: http://localhost:8080/health
The Docker Compose setup includes:
- mqtt_manager: Flask application (port 8080)
- postgres: PostgreSQL database (port 5432)
- Persistent volumes for database data
- Health checks for all services
- Automatic restart on failure
POST /api/mqtt/start- Start MQTT serverPOST /api/mqtt/stop- Stop MQTT serverPOST /api/mqtt/restart- Restart MQTT serverGET /api/mqtt/status- Get current MQTT status
GET /api/health/current- Get current system healthGET /api/health/mqtt- Get latest MQTT health from storageGET /api/health/history/system?limit=100- Get system health historyGET /api/health/history/mqtt?limit=100- Get MQTT status historyGET /api/health/combined- Get combined health data
Run the test suite:
# Run all tests
pytest
# Run with coverage
pytest --cov=src --cov-report=html
# Run specific test file
pytest tests/test_config.pyConfigManager: Centralized configuration with dot-notation access- Supports YAML configuration files
- Hot-reload capability
MQTTStatus: Type-safe MQTT server status modelSystemHealth: System resource usage model- Built with Pydantic for validation
PersistenceStore: Abstract base classJSONStore: File-based storage implementationPostgresStore: PostgreSQL database implementation- Automatic fallback from PostgreSQL to JSON
MQTTManager: MQTT server lifecycle managementHealthMonitor: System resource monitoringStatusSaver: Periodic status persistence (background thread)
mqtt_routes: MQTT server control endpointshealth_routes: Health monitoring endpoints- Factory pattern for blueprint creation
- Plain HTML/CSS/JavaScript
- Auto-refreshing dashboard (5-second interval)
- Real-time progress bars and status indicators
- Responsive design
- Dependency Injection: Services receive dependencies via constructors
- Factory Pattern: Route blueprints created via factory functions
- Strategy Pattern: Persistence layer with interchangeable backends
- Observer Pattern: Background thread for periodic status updates
- Singleton Pattern: Configuration manager instance
- Type hints throughout the codebase
- Comprehensive docstrings (Google style)
- Clean code principles (DRY, SOLID)
- Error handling and logging
- Resource cleanup on shutdown
- Thread-safe operations
Container won't start:
# Check logs
docker-compose logs mqtt_manager
# Rebuild from scratch
docker-compose down -v
docker-compose build --no-cache
docker-compose up -dDatabase connection issues:
# Check PostgreSQL is running
docker-compose ps postgres
# Check database logs
docker-compose logs postgres
# Restart database
docker-compose restart postgresPort conflicts:
If ports 8080 or 5432 are already in use, modify docker-compose.yml:
ports:
- "8081:8080" # Change external portThe application automatically falls back to JSON storage if PostgreSQL is unavailable.
- Check that Mosquitto is installed:
mosquitto -h - Verify the command in
config.yaml - Check screen is installed:
screen --version - Review logs for specific errors
- Check browser console for API errors
- Verify backend is running
- Check CORS settings
See LICENSE file in the repository root.
This project follows clean code principles. When contributing:
- Add type hints to all functions
- Write docstrings for public methods
- Add tests for new features
- Follow existing code style
- Update documentation as needed