From 991bae50f6127fecb112ab7269cf2022e9596a0a Mon Sep 17 00:00:00 2001 From: prplV Date: Thu, 16 Jan 2025 15:12:35 +0300 Subject: [PATCH] unixsocket fix + disable lint warnings --- crates/api-grub/Cargo.toml | 2 +- crates/api-grub/src/config.rs | 44 +++++++++++++++++++++-------------- crates/api-grub/src/main.rs | 10 +++++++- crates/api-grub/src/net.rs | 12 +++++++--- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/crates/api-grub/Cargo.toml b/crates/api-grub/Cargo.toml index 287df71..85ab777 100644 --- a/crates/api-grub/Cargo.toml +++ b/crates/api-grub/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "api-grub" -version = "0.1.0" +version = "0.3.8" edition = "2021" [dependencies] diff --git a/crates/api-grub/src/config.rs b/crates/api-grub/src/config.rs index ae38016..b9c8e17 100644 --- a/crates/api-grub/src/config.rs +++ b/crates/api-grub/src/config.rs @@ -3,22 +3,24 @@ // 2) save changes in local config file use integr_structs::api::ApiConfig; use anyhow::{Error, Ok, Result}; -use log::{info, warn}; +use log::{info, warn, error}; use std::{fs, path::Path}; use serde_json::from_str; use tokio::{io::AsyncReadExt, net::UnixListener}; -use std::io; use tokio::time::{sleep, Duration}; use std::result::Result::Ok as stdOk; +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 { - let conf_path = std::env::current_exe()?; - let path = Path::new("config_api.json"); + // let conf_path = std::env::current_exe()?; + let path = Path::new(CONFIG_PATH); // return match conf_path.parent() { // Some(dir) => { // let config: ApiConfig = from_str( - // &fs::read_to_string(dir.join("config_api.json"))? + // &fs::read_to_string(dir.join(CONFIG_PATH))? // )?; // Ok(config) // }, @@ -26,7 +28,7 @@ pub async fn pull_local_config() -> Result { // } if path.exists() && path.is_file() { let config: ApiConfig = from_str( - &fs::read_to_string("config_api.json")? + &fs::read_to_string(CONFIG_PATH)? )?; Ok(config) } else { @@ -36,7 +38,8 @@ pub async fn pull_local_config() -> Result { // for config pulling // ++++ reader to channel -async fn init_config_grub_mechanism(config: ApiConfig) -> Result<()> { +pub async fn init_config_grub_mechanism() -> Result<()> { + info!("Initializing Unix-Socket listening for pulling new configs..."); let server = init_unix_listener().await?; // info!("Listening Unix-Socket..."); @@ -46,26 +49,33 @@ async fn init_config_grub_mechanism(config: ApiConfig) -> Result<()> { if let stdOk((mut stream, _)) = server.accept().await { if let Err(er) = stream.read_to_string(&mut buffer).await { warn!("Cannot read config from stream due to {}", er); - sleep(Duration::from_millis(500)).await; } else { let config: Result = from_str(&buffer); - if let stdOk(conf) = config { - - } else { - + if let stdOk(_conf) = config { + info!("New config was pulled from Unix-Stream. Saving it locally..."); + if let Err(er) = save_new_config(&buffer).await { + error!("Cannot save new config locally due to: {}", er); + } + // TODO! + // reading new config to channel + // saving new config locally + } else if let Err(er) = config { + warn!("Invalid config was pulled. Error: {}", er); } } } + sleep(Duration::from_millis(500)).await; } - // - Ok(()) } -async fn save_new_config() -> Result<()> { + +// saving new config locally +async fn save_new_config(config: &String) -> Result<()> { + fs::write(CONFIG_PATH, config)?; Ok(()) } async fn init_unix_listener() -> Result { - fs::remove_file("api-grub.sock")?; - Ok(UnixListener::bind("api-grub.sock")?) + let _ = fs::remove_file(SOCKET_PATH); + Ok(UnixListener::bind(SOCKET_PATH)?) } \ No newline at end of file diff --git a/crates/api-grub/src/main.rs b/crates/api-grub/src/main.rs index 6ebc2f7..8c8b14f 100644 --- a/crates/api-grub/src/main.rs +++ b/crates/api-grub/src/main.rs @@ -6,7 +6,8 @@ use anyhow::Result; use integr_structs::api::ApiConfig; use logger::setup_logger; use log::{info, warn}; -use config::pull_local_config; +use config::{pull_local_config, init_config_grub_mechanism}; +use net::init_api_grub_mechanism; #[tokio::main(flavor = "multi_thread")] async fn main() -> Result<()>{ @@ -16,6 +17,13 @@ async fn main() -> Result<()>{ // 3) ? setup_logger().await?; let config = get_config().await; + + // futures + let config_fut = init_config_grub_mechanism(); + let grub_fut = init_api_grub_mechanism(&config); + + let _ = tokio::join!(config_fut, grub_fut); + Ok(()) } diff --git a/crates/api-grub/src/net.rs b/crates/api-grub/src/net.rs index d05fd78..a83a515 100644 --- a/crates/api-grub/src/net.rs +++ b/crates/api-grub/src/net.rs @@ -1,9 +1,15 @@ // module to handle unix-socket connection + pulling info from api -use anyhow; +use anyhow::Result; +use integr_structs::api::ApiConfig; +use log::info; // for api info pulling -async fn init_api_grub_mechanism() { - +pub async fn init_api_grub_mechanism(_config: &ApiConfig) -> Result<()> { + info!("Initializing API-info grubbing mechanism..."); + Ok(()) } +// one-time exec func to send request, deserialize it and return object +#[allow(dead_code)] +async fn send_api_request() -> Result<()> {Ok(())} \ No newline at end of file