From 3ebf40aa64e18faa8c688eaa19f744c73ee025bc Mon Sep 17 00:00:00 2001 From: prplV Date: Thu, 13 Feb 2025 13:01:07 +0300 Subject: [PATCH] bugged: config v3 --- crates/api-grub/src/config.rs | 13 +++++++------ crates/api-grub/src/main.rs | 5 +++-- crates/integr-structs/src/api.rs | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/api-grub/src/config.rs b/crates/api-grub/src/config.rs index 11fe491..b945d31 100644 --- a/crates/api-grub/src/config.rs +++ b/crates/api-grub/src/config.rs @@ -10,12 +10,13 @@ use tokio::{io::AsyncReadExt, net::UnixListener}; use tokio::time::{sleep, Duration}; use std::result::Result::Ok as stdOk; use tokio::sync::mpsc::Sender; +use integr_structs::api::v3::Config; const CONFIG_PATH: &str = "config_api.json"; const SOCKET_PATH: &str = "api-grub.sock"; // todo! rewrite to use current_exe -pub async fn pull_local_config() -> Result { +pub async fn pull_local_config() -> Result { // let conf_path = std::env::current_exe()?; let path = Path::new(CONFIG_PATH); // return match conf_path.parent() { @@ -28,7 +29,7 @@ pub async fn pull_local_config() -> Result { // None => Err(Error::msg("No local conf was found")) // } if path.exists() && path.is_file() { - let config: ApiConfigV2 = from_str( + let config: Config = from_str( &fs::read_to_string(CONFIG_PATH)? )?; Ok(config) @@ -39,7 +40,7 @@ pub async fn pull_local_config() -> Result { // for config pulling // ++++ reader to channel -pub async fn init_config_grub_mechanism(tx: &Sender) -> Result<()> { +pub async fn init_config_grub_mechanism(tx: &Sender) -> Result<()> { info!("Initializing Unix-Socket listening for pulling new configs..."); let server = init_unix_listener().await?; // @@ -51,7 +52,7 @@ pub async fn init_config_grub_mechanism(tx: &Sender) -> Result<()> if let Err(er) = stream.read_to_string(&mut buffer).await { warn!("Cannot read config from stream due to {}", er); } else { - let config: Result = from_str(&buffer); + let config: Result = from_str(&buffer); if let stdOk(conf) = config { info!("New config was pulled from Unix-Stream. Saving it locally and sharing with API-grub module..."); if let Err(er) = save_new_config(&buffer).await { @@ -97,13 +98,13 @@ mod config_unittests { #[test] async fn check_save_new_config() { use std::fs; - use integr_structs::api::ApiConfigV2; + use integr_structs::api::v3::Config; use serde_json::to_string; let test_config_path = "test_config_api.json"; // config gen - let config = to_string::(&ApiConfigV2::default()); + let config = to_string::(&Config::default()); assert!(config.is_ok()); let config = config.unwrap(); diff --git a/crates/api-grub/src/main.rs b/crates/api-grub/src/main.rs index f35511c..1db6f64 100644 --- a/crates/api-grub/src/main.rs +++ b/crates/api-grub/src/main.rs @@ -6,6 +6,7 @@ mod export; use anyhow::Result; use integr_structs::api::ApiConfigV2; +use integr_structs::api::v3::Config; use logger::setup_logger; // use log::{info, warn}; use config::{pull_local_config, init_config_grub_mechanism}; @@ -57,7 +58,7 @@ async fn main() -> Result<()>{ Ok(()) } -async fn get_config() -> ApiConfigV2 { +async fn get_config() -> Config { return match pull_local_config().await { Ok(conf) => { info!("Local config was loaded"); @@ -65,7 +66,7 @@ async fn get_config() -> ApiConfigV2 { }, Err(er) => { warn!("Cannot get local config due to {}", er); - ApiConfigV2::default() + Config::default() } } } \ No newline at end of file diff --git a/crates/integr-structs/src/api.rs b/crates/integr-structs/src/api.rs index f26850b..02ca610 100644 --- a/crates/integr-structs/src/api.rs +++ b/crates/integr-structs/src/api.rs @@ -150,14 +150,14 @@ pub mod v3 { pub use super::*; // in config - #[derive(Deserialize)] + #[derive(Serialize, Deserialize)] pub struct Metric { pub id : String, #[serde(rename = "type")] pub json_type : String, pub addr : String, } - #[derive(Deserialize)] + #[derive(Serialize, Deserialize)] pub struct Metrics { pub name : String, pub url : String, @@ -165,7 +165,7 @@ pub mod v3 { pub measure : Vec } - #[derive(Deserialize)] + #[derive(Serialize, Deserialize)] pub struct ConfigEndpoint { ip : String, login : String, @@ -178,7 +178,7 @@ pub mod v3 { metrics : Vec, } - #[derive(Deserialize)] + #[derive(Serialize, Deserialize)] pub struct Config { config : Vec, }