Skip to content

Commit

Permalink
feat: replaced toml-env with figment
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfdl committed Jun 22, 2024
1 parent 12c0c5c commit 5fc3b5f
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 80 deletions.
109 changes: 87 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ serde = "1.0.203"
serde_json = "1.0.117"
tokio = {version = "1.37.0", features = ["full", "rt-multi-thread"]}
tokio-rustls = "0.26.0"
toml-env = "1.2.0"
figment = { version = "0.10", features = ["toml", "env"] }
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ export TAGOIO__RELAY__MQTT__BROKER_TLS_CERT=""
export TAGOIO__RELAY__MQTT__BROKER_TLS_KEY=""

# Subscribe to multiple topics
export TAGOIO__RELAY__MQTT__SUBSCRIBE__1="/tago/#"
export TAGOIO__RELAY__MQTT__SUBSCRIBE__2="/topic/+"
export TAGOIO__RELAY__MQTT__SUBSCRIBE=["/tago/#"]

# Change the path to the configuration file
export TAGOIO__RELAY__CONFIG_PATH="/root/.config/.tagoio-mqtt-relay.toml"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ services:
# - TAGOIO__RELAY__MQTT__TLS_ENABLED="false"
# - TAGOIO__RELAY__MQTT__ADDRESS="localhost"
# - TAGOIO__RELAY__MQTT__PORT="1883"
# - TAGOIO__RELAY__MQTT__SUBSCRIBE__1="/device/#"
# - TAGOIO__RELAY__MQTT__SUBSCRIBE=["/device/#"]
# - TAGOIO__RELAY__MQTT__USERNAME="my-username"
# - TAGOIO__RELAY__MQTT__PASSWORD="my-password"
# - TAGOIO__RELAY__MQTT__BROKER_TLS_CA=""
Expand Down
7 changes: 3 additions & 4 deletions examples/mosquitto-relay-setup/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ services:

# Mosquitto Broker Settings for the Relay to connect to
- TAGOIO__RELAY__MQTT__ADDRESS=mosquitto-broker
- TAGOIO__RELAY__MQTT__TLS_ENABLED=false
- TAGOIO__RELAY__MQTT__PORT=1883
- TAGOIO__RELAY__MQTT__CLIENT_ID=tagoio-relay
- TAGOIO__RELAY__MQTT__USERNAME=my-username
- TAGOIO__RELAY__MQTT__PASSWORD=my-password
- TAGOIO__RELAY__MQTT__SUBSCRIBE__1="/tago/#"

# - TAGOIO__RELAY__MQTT__PORT=1883
- TAGOIO__RELAY__MQTT__SUBSCRIBE=["/device/#"]
- TAGOIO__RELAY__MQTT__TLS_ENABLED=false
# - TAGOIO__RELAY__MQTT__BROKER_TLS_CA=""
# - TAGOIO__RELAY__MQTT__BROKER_TLS_CERT=""
# - TAGOIO__RELAY__MQTT__BROKER_TLS_KEY=""
Expand Down
4 changes: 2 additions & 2 deletions src/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
network_token="Your-Network-Token" # Generate a Network Token under your TagoIO Network Settings
authorization_token="Your-Authorization-Token" # Generate an Authorization Token under your TagoIO > Devices > Authorizations
tagoio_url="https://api.tago.io" # Default
downlink_port="3001" # Default is 3000
downlink_port=3001 # Default is 3000

[relay.mqtt]
client_id="tagoio-relay" # Default is tagoio-relay
tls_enabled=false
address="localhost"
port="1883"
port=1883
subscribe=["/tago/#", "/tago/+"] # MQTT topics to subscribe to
username="my-username"
password="my-passowrd"
Expand Down
12 changes: 2 additions & 10 deletions src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ pub async fn start_relay() -> Result<()> {

let api_port = {
let config_file = CONFIG_FILE.read().unwrap();
config_file
.as_ref()
.unwrap()
.downlink_port
.clone()
.unwrap_or("3000".to_string())
config_file.as_ref().unwrap().downlink_port.unwrap_or(3000)
};

let test = create_ssl_acceptor().unwrap();
Expand All @@ -115,10 +110,7 @@ pub async fn start_relay() -> Result<()> {
// }
// };

let addr = SocketAddr::from((
HOST_ADDRESS.parse::<std::net::IpAddr>().unwrap(),
api_port.parse::<u16>().unwrap(),
));
let addr = SocketAddr::from((HOST_ADDRESS.parse::<std::net::IpAddr>().unwrap(), api_port));

tokio::spawn(async move {
log::info!(target: "info", "Starting Publish API on: {}", addr);
Expand Down
18 changes: 9 additions & 9 deletions src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct RelayConfig {
pub struct ConfigFile {
pub network_token: String,
pub authorization_token: String,
pub tagoio_url: Option<String>, // Default is "https://api.tago.io"
pub downlink_port: Option<String>, // Default is "3000"
pub tagoio_url: Option<String>, // Default is "https://api.tago.io"
pub downlink_port: Option<u16>, // Default is "3000"
pub mqtt: Mqtt,
}

Expand All @@ -20,7 +20,7 @@ pub struct Mqtt {
pub client_id: Option<String>, // Default is "tagoio-relay"
pub tls_enabled: bool,
pub address: String,
pub port: String,
pub port: u16,
pub subscribe: Vec<String>, // Default is ["/tago/#", "/device/+"]
pub username: Option<String>, // Default is "my-username"
pub password: Option<String>, // Default is "my-password"
Expand Down Expand Up @@ -53,7 +53,7 @@ impl ConfigFile {
self.tagoio_url = Some("https://api.tago.io".to_string());
}
if self.downlink_port.is_none() {
self.downlink_port = Some("3000".to_string());
self.downlink_port = Option::from(3000);
}
self.mqtt = self.mqtt.with_defaults()?;
Ok(self)
Expand Down Expand Up @@ -94,7 +94,7 @@ mod tests {
client_id: None,
tls_enabled: false,
address: "localhost".to_string(),
port: "1883".to_string(),
port: 1883,
subscribe: vec![],
username: None,
password: None,
Expand All @@ -110,7 +110,7 @@ mod tests {
assert_eq!(relay_config.profile_id.unwrap(), "self-hosted");
// assert_eq!(relay_config.state.unwrap(), InitiatedState::Stopped);
assert_eq!(relay_config.config.tagoio_url.unwrap(), "https://api.tago.io");
assert_eq!(relay_config.config.downlink_port.unwrap(), "3000");
assert_eq!(relay_config.config.downlink_port.unwrap(), 3000);
assert_eq!(relay_config.config.mqtt.client_id.unwrap(), "tagoio-relay");
// assert_eq!(
// relay_config.config.mqtt.authentication_certificate_file.unwrap(),
Expand All @@ -129,7 +129,7 @@ mod tests {
client_id: None,
tls_enabled: false,
address: "localhost".to_string(),
port: "1883".to_string(),
port: 1883,
subscribe: vec![],
username: None,
password: None,
Expand All @@ -142,7 +142,7 @@ mod tests {
let config_with_defaults = config.with_defaults().unwrap();

assert_eq!(config_with_defaults.tagoio_url.unwrap(), "https://api.tago.io");
assert_eq!(config_with_defaults.downlink_port.unwrap(), "3000");
assert_eq!(config_with_defaults.downlink_port.unwrap(), 3000);
assert_eq!(config_with_defaults.mqtt.client_id.unwrap(), "tagoio-relay");
}

Expand All @@ -152,7 +152,7 @@ mod tests {
client_id: None,
tls_enabled: false,
address: "localhost".to_string(),
port: "1883".to_string(),
port: 1883,
subscribe: vec!["/tago/#".to_string(), "/device/+".to_string()],
username: None,
password: None,
Expand Down
10 changes: 1 addition & 9 deletions src/services/mqttrelay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,7 @@ fn initialize_mqtt_options(relay_cfg: &RelayConfig) -> MqttOptions {
let crt_file = &relay_cfg.config.mqtt.broker_tls_cert;
let key_file = &relay_cfg.config.mqtt.broker_tls_key;

let port: u16 = match relay_cfg.config.mqtt.port.parse() {
Ok(port) => port,
Err(e) => {
log::error!(target: "mqtt", "Invalid Broker Port number: {}. Error: {:?}", relay_cfg.config.mqtt.port, e);
std::process::exit(1);
}
};

let mut mqttoptions = MqttOptions::new(client_id, &relay_cfg.config.mqtt.address, port);
let mut mqttoptions = MqttOptions::new(client_id, &relay_cfg.config.mqtt.address, relay_cfg.config.mqtt.port);
mqttoptions.set_keep_alive(Duration::from_secs(30));
mqttoptions.set_max_packet_size(1024 * 1024, 1024 * 1024); // 1mb in/out

Expand Down
4 changes: 2 additions & 2 deletions src/services/tagoio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ mod tests {
network_token: "test_network_token".to_string(),
authorization_token: "test_authorization_token".to_string(),
tagoio_url: Some(server.url()),
downlink_port: Some("3000".to_string()),
downlink_port: Some(3000),
mqtt: Mqtt {
client_id: Some("test_client_id".to_string()),
tls_enabled: false,
address: "localhost".to_string(),
port: "1883".to_string(),
port: 1883,
subscribe: vec!["/tago/#".to_string(), "/device/+".to_string()],
username: Some("test_username".to_string()),
password: Some("test_password".to_string()),
Expand Down
Loading

0 comments on commit 5fc3b5f

Please sign in to comment.