From cfdfa883318840a3ec69b90a8ab5384b7c4b52e6 Mon Sep 17 00:00:00 2001 From: a-wing <1@233.email> Date: Tue, 26 Nov 2024 23:30:50 +0800 Subject: [PATCH] feat(net4mqtt): conf use alias replace template mqtt_url --- conf/live777.toml | 5 ++++- conf/liveman.toml | 4 +++- liveion/src/config.rs | 7 +++++++ liveion/src/lib.rs | 3 ++- liveman/src/config.rs | 7 +++++++ liveman/src/lib.rs | 3 ++- 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/conf/live777.toml b/conf/live777.toml index fcfaeff..32477df 100644 --- a/conf/live777.toml +++ b/conf/live777.toml @@ -59,5 +59,8 @@ urls = [ # Default enabled `--features=net4mqtt` # [net4mqtt] -# mqtt_url = "mqtt://localhost:1883/net4mqtt" +# Global unique alias # alias = "liveion-0" +# `client_id={alias}` use alias as MQTT `client_id` +# mqtt_url = "mqtt://localhost:1883/net4mqtt?client_id={alias}" + diff --git a/conf/liveman.toml b/conf/liveman.toml index d44819c..0b5b0ce 100644 --- a/conf/liveman.toml +++ b/conf/liveman.toml @@ -41,8 +41,10 @@ # close_other_sub = false # [net4mqtt] -# mqtt_url = "mqtt://localhost:1883/net4mqtt" +# Global unique alias # alias = "liveman-0" +# `client_id={alias}` use alias as MQTT `client_id` +# mqtt_url = "mqtt://localhost:1883/net4mqtt?client_id={alias}" # listen = "127.0.0.1:1077" # domain = "net4mqtt.local" diff --git a/liveion/src/config.rs b/liveion/src/config.rs index fc32f5e..39f41be 100644 --- a/liveion/src/config.rs +++ b/liveion/src/config.rs @@ -37,6 +37,13 @@ pub struct Net4mqtt { pub alias: String, } +#[cfg(feature = "net4mqtt")] +impl Net4mqtt { + pub fn validate(&mut self) { + self.mqtt_url = self.mqtt_url.replace("{alias}", &self.alias) + } +} + #[derive(Debug, Default, Clone, Deserialize, Serialize)] pub struct Webhook { #[serde(default)] diff --git a/liveion/src/lib.rs b/liveion/src/lib.rs index 7348afb..8ebe78d 100644 --- a/liveion/src/lib.rs +++ b/liveion/src/lib.rs @@ -84,7 +84,8 @@ where #[cfg(feature = "net4mqtt")] { - if let Some(c) = cfg.net4mqtt { + if let Some(mut c) = cfg.net4mqtt { + c.validate(); std::thread::spawn(move || { tokio::runtime::Runtime::new() .unwrap() diff --git a/liveman/src/config.rs b/liveman/src/config.rs index c2d6f80..8a32b87 100644 --- a/liveman/src/config.rs +++ b/liveman/src/config.rs @@ -46,6 +46,13 @@ pub struct Net4mqtt { pub domain: String, } +#[cfg(feature = "net4mqtt")] +impl Net4mqtt { + pub fn validate(&mut self) { + self.mqtt_url = self.mqtt_url.replace("{alias}", &self.alias) + } +} + #[cfg(feature = "net4mqtt")] impl Default for Net4mqtt { fn default() -> Self { diff --git a/liveman/src/lib.rs b/liveman/src/lib.rs index 9a040a1..8f953da 100644 --- a/liveman/src/lib.rs +++ b/liveman/src/lib.rs @@ -71,7 +71,8 @@ where #[cfg(feature = "net4mqtt")] { - if let Some(c) = cfg.net4mqtt.clone() { + if let Some(mut c) = cfg.net4mqtt.clone() { + c.validate(); let (sender, mut receiver) = tokio::sync::mpsc::channel::<(String, String, Vec)>(10);