Skip to content

Commit ca5c555

Browse files
committed
Standardize configuration to yaml.
#2
1 parent de0b48a commit ca5c555

File tree

5 files changed

+98
-75
lines changed

5 files changed

+98
-75
lines changed

tools/developer_tools/bely-mqtt-message-broker/config.example.json

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# BELY MQTT Framework Configuration
2+
# This file configures global settings and handler-specific parameters
3+
4+
# Global configuration shared across all handlers
5+
global:
6+
# BELY API URL for querying additional information
7+
bely_url: https://bely.gov/bely
8+
9+
# Handler-specific configurations
10+
handlers:
11+
# Advanced logging handler with custom directory
12+
AdvancedLoggingHandler:
13+
logging_dir: /var/log/bely
14+
15+
# Basic logging handler (no configuration needed)
16+
LoggingHandler: {}
17+
18+
# Notification handler with webhook
19+
NotificationHandler:
20+
webhook_url: https://example.com/webhook
21+
22+
# Apprise smart notification handler
23+
AppriseSmartNotificationHandler:
24+
config:
25+
config_path: /path/to/apprise_notification_config.yaml

tools/developer_tools/bely-mqtt-message-broker/docs/configuration.md

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ bely-mqtt start [OPTIONS]
1010

1111
### MQTT Options
1212

13-
- `--mqtt-host TEXT` - MQTT broker hostname (default: localhost)
14-
- `--mqtt-port INTEGER` - MQTT broker port (default: 1883)
15-
- `--mqtt-username TEXT` - MQTT username for authentication
16-
- `--mqtt-password TEXT` - MQTT password for authentication
17-
- `--mqtt-topic TEXT` - MQTT topic pattern to subscribe (default: bely/#)
13+
- `--broker-host TEXT` - MQTT broker hostname (default: localhost)
14+
- `--broker-port INTEGER` - MQTT broker port (default: 1883)
15+
- `--username TEXT` - MQTT username for authentication
16+
- `--password TEXT` - MQTT password for authentication
17+
- `--topic TEXT` - MQTT topic pattern to subscribe (default: bely/#)
1818

1919
### Handler Options
2020

2121
- `--handlers-dir PATH` - Directory containing handler files (default: ./handlers)
22-
- `--handler-config PATH` - JSON configuration file for handlers
22+
- `--config PATH` - YAML configuration file for handlers
2323

2424
### API Options
2525

@@ -29,33 +29,37 @@ bely-mqtt start [OPTIONS]
2929
### Logging Options
3030

3131
- `--log-level TEXT` - Logging level: DEBUG, INFO, WARNING, ERROR (default: INFO)
32-
- `--log-file PATH` - Log to file instead of console
3332

3433
## Environment Variables
3534

3635
All command-line options can be set via environment variables:
3736

3837
```bash
39-
export MQTT_HOST=broker.example.com
40-
export MQTT_PORT=1883
38+
export MQTT_BROKER_HOST=broker.example.com
39+
export MQTT_BROKER_PORT=1883
40+
export MQTT_CLIENT_ID=bely-mqtt-client
4141
export MQTT_USERNAME=myuser
4242
export MQTT_PASSWORD=mypass
4343
export BELY_API_URL=https://api.bely.dev
4444
export BELY_API_KEY=your-api-key
45+
export BELY_HANDLERS_DIR=./handlers
46+
export BELY_CONFIG=./config.yaml
4547
export LOG_LEVEL=DEBUG
4648
```
4749

48-
## Configuration File
50+
## Configuration Files
4951

50-
Create a `.env` file in your project root:
52+
### Environment File (.env)
53+
54+
Create a `.env` file in your project root for environment variables:
5155

5256
```bash
5357
# MQTT Configuration
54-
MQTT_HOST=localhost
55-
MQTT_PORT=1883
58+
MQTT_BROKER_HOST=localhost
59+
MQTT_BROKER_PORT=1883
60+
MQTT_CLIENT_ID=bely-mqtt-client
5661
MQTT_USERNAME=
5762
MQTT_PASSWORD=
58-
MQTT_TOPIC=bely/#
5963

6064
# BELY API Configuration
6165
BELY_API_URL=https://api.bely.dev
@@ -65,29 +69,43 @@ BELY_API_KEY=your-api-key-here
6569
LOG_LEVEL=INFO
6670

6771
# Handler Configuration
68-
HANDLERS_DIR=./handlers
72+
BELY_HANDLERS_DIR=./handlers
73+
BELY_CONFIG=./config.yaml
6974
```
7075

71-
## Handler Configuration
72-
73-
Handlers can be configured via JSON file:
74-
75-
```json
76-
{
77-
"handlers": [
78-
{
79-
"module": "notification_handler",
80-
"class": "NotificationHandler",
81-
"config": {
82-
"webhook_url": "https://hooks.slack.com/services/YOUR/WEBHOOK",
83-
"enabled": true
84-
}
85-
}
86-
]
87-
}
76+
### Handler Configuration (YAML)
77+
78+
Handlers can be configured via a YAML file to provide both global and handler-specific settings:
79+
80+
```yaml
81+
# Global configuration shared across all handlers
82+
global:
83+
# BELY API URL for querying additional information
84+
bely_url: https://bely.example.com/bely
85+
# Add any other global parameters here
86+
shared_param: value
87+
88+
# Handler-specific configurations
89+
handlers:
90+
# Configure the AdvancedLoggingHandler
91+
AdvancedLoggingHandler:
92+
logging_dir: /var/log/bely
93+
log_level: DEBUG
94+
rotate_logs: true
95+
max_size_mb: 100
96+
97+
# Configure the NotificationHandler
98+
NotificationHandler:
99+
webhook_url: https://hooks.slack.com/services/YOUR/WEBHOOK
100+
enabled: true
101+
timeout: 30
102+
103+
# Configure the AppriseSmartNotificationHandler
104+
AppriseSmartNotificationHandler:
105+
config_path: /path/to/apprise_notification_config.yaml
88106
```
89107
90-
Use with: `--handler-config config.json`
108+
Use with: `--config config.yaml`
91109

92110
## SSL/TLS Configuration
93111

tools/developer_tools/bely-mqtt-message-broker/src/bely_mqtt/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def cli() -> None:
106106
type=click.Path(exists=True, file_okay=True, dir_okay=False, path_type=Path),
107107
default=None,
108108
envvar="BELY_CONFIG",
109-
help="Path to configuration file for handlers (JSON format).",
109+
help="Path to configuration file for handlers (YAML format).",
110110
)
111111
def start(
112112
broker_host: str,

tools/developer_tools/bely-mqtt-message-broker/src/bely_mqtt/config.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
configuration parameters when they are instantiated.
66
"""
77

8-
import json
98
import logging
109
from pathlib import Path
1110
from typing import Any, Dict, Optional
1211

12+
import yaml
13+
1314
logger = logging.getLogger(__name__)
1415

1516

@@ -110,39 +111,37 @@ def __init__(self):
110111

111112
def load_from_file(self, config_file: Path) -> None:
112113
"""
113-
Load configuration from a JSON file.
114+
Load configuration from a YAML file.
114115
115116
File format:
116-
{
117-
"global": {
118-
"shared_param": "value",
119-
"another_param": "value"
120-
},
121-
"handlers": {
122-
"AdvancedLoggingHandler": {
123-
"logging_dir": "/var/log/bely"
124-
},
125-
"MyHandler": {
126-
"param1": "value1",
127-
"param2": "value2"
128-
}
129-
}
130-
}
117+
global:
118+
shared_param: value
119+
another_param: value
120+
bely_url: https://bely.example.com
121+
122+
handlers:
123+
AdvancedLoggingHandler:
124+
logging_dir: /var/log/bely
125+
MyHandler:
126+
param1: value1
127+
param2: value2
128+
AppriseSmartNotificationHandler:
129+
config_path: /path/to/apprise_config.yaml
131130
132131
Args:
133-
config_file: Path to the configuration file.
132+
config_file: Path to the YAML configuration file.
134133
135134
Raises:
136135
FileNotFoundError: If the configuration file doesn't exist.
137-
json.JSONDecodeError: If the file is not valid JSON.
136+
yaml.YAMLError: If the file is not valid YAML.
138137
"""
139138
config_file = Path(config_file)
140139
if not config_file.exists():
141140
raise FileNotFoundError(f"Configuration file not found: {config_file}")
142141

143142
try:
144143
with open(config_file) as f:
145-
data = json.load(f)
144+
data = yaml.safe_load(f)
146145

147146
# Load global configuration if present
148147
if "global" in data:
@@ -155,8 +154,8 @@ def load_from_file(self, config_file: Path) -> None:
155154
self.set_config(handler_name, config)
156155

157156
self.logger.info(f"Loaded configuration from {config_file}")
158-
except json.JSONDecodeError as e:
159-
self.logger.error(f"Invalid JSON in configuration file: {e}")
157+
except yaml.YAMLError as e:
158+
self.logger.error(f"Invalid YAML in configuration file: {e}")
160159
raise
161160

162161
def load_from_dict(self, config_dict: Dict[str, Any]) -> None:

0 commit comments

Comments
 (0)