diff --git a/crates/api-grub/src/config.rs b/crates/api-grub/src/config.rs index 3455016..ae38016 100644 --- a/crates/api-grub/src/config.rs +++ b/crates/api-grub/src/config.rs @@ -2,13 +2,70 @@ // 1) check changes in unix-socket // 2) save changes in local config file use integr_structs::api::ApiConfig; -use tokio::net::UnixListener; -use anyhow::Result; +use anyhow::{Error, Ok, Result}; +use log::{info, warn}; +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; + +// 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"); + // return match conf_path.parent() { + // Some(dir) => { + // let config: ApiConfig = from_str( + // &fs::read_to_string(dir.join("config_api.json"))? + // )?; + // Ok(config) + // }, + // None => Err(Error::msg("No local conf was found")) + // } + if path.exists() && path.is_file() { + let config: ApiConfig = from_str( + &fs::read_to_string("config_api.json")? + )?; + Ok(config) + } else { + Err(Error::msg("No local conf was found")) + } +} // for config pulling -async fn init_api_grub_mechanism(config: ApiConfig) { +// ++++ reader to channel +async fn init_config_grub_mechanism(config: ApiConfig) -> Result<()> { + let server = init_unix_listener().await?; + // + info!("Listening Unix-Socket..."); + let mut buffer = String::new(); + // + loop { + 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 { + + } + } + } + } + // + Ok(()) +} +async fn save_new_config() -> Result<()> { + Ok(()) } -async fn init_unix_listener() {} \ No newline at end of file +async fn init_unix_listener() -> Result { + fs::remove_file("api-grub.sock")?; + Ok(UnixListener::bind("api-grub.sock")?) +} \ No newline at end of file