diff --git a/crates/api-grub/Cargo.toml b/crates/api-grub/Cargo.toml index 85ab777..8c51835 100644 --- a/crates/api-grub/Cargo.toml +++ b/crates/api-grub/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "api-grub" -version = "0.3.8" +version = "0.3.13" edition = "2021" [dependencies] @@ -12,3 +12,4 @@ env_logger = "0.11.6" log = "0.4.25" anyhow = "1.0.95" chrono = "0.4.39" +reqwest = { version = "0.12.12", features = ["rustls-tls", "json"] } \ No newline at end of file diff --git a/crates/api-grub/src/config.rs b/crates/api-grub/src/config.rs index b9c8e17..cfe96b7 100644 --- a/crates/api-grub/src/config.rs +++ b/crates/api-grub/src/config.rs @@ -9,6 +9,7 @@ use serde_json::from_str; use tokio::{io::AsyncReadExt, net::UnixListener}; use tokio::time::{sleep, Duration}; use std::result::Result::Ok as stdOk; +use tokio::sync::mpsc::Sender; const CONFIG_PATH: &str = "config_api.json"; const SOCKET_PATH: &str = "api-grub.sock"; @@ -38,7 +39,7 @@ pub async fn pull_local_config() -> Result { // for config pulling // ++++ reader to channel -pub async fn init_config_grub_mechanism() -> 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,14 +52,12 @@ pub async fn init_config_grub_mechanism() -> Result<()> { warn!("Cannot read config from stream due to {}", er); } else { let config: Result = from_str(&buffer); - if let stdOk(_conf) = config { - info!("New config was pulled from Unix-Stream. Saving it locally..."); + 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 { error!("Cannot save new config locally due to: {}", er); } - // TODO! - // reading new config to channel - // saving new config locally + let _ = tx.send(conf).await; } else if let Err(er) = config { warn!("Invalid config was pulled. Error: {}", er); } diff --git a/crates/api-grub/src/main.rs b/crates/api-grub/src/main.rs index 8c8b14f..208d646 100644 --- a/crates/api-grub/src/main.rs +++ b/crates/api-grub/src/main.rs @@ -8,6 +8,7 @@ use logger::setup_logger; use log::{info, warn}; use config::{pull_local_config, init_config_grub_mechanism}; use net::init_api_grub_mechanism; +use tokio::sync::mpsc; #[tokio::main(flavor = "multi_thread")] async fn main() -> Result<()>{ @@ -17,10 +18,11 @@ async fn main() -> Result<()>{ // 3) ? setup_logger().await?; let config = get_config().await; - + // config update channel + let (tx, mut rx) = mpsc::channel::(1); // futures - let config_fut = init_config_grub_mechanism(); - let grub_fut = init_api_grub_mechanism(&config); + let config_fut = init_config_grub_mechanism(&tx); + let grub_fut = init_api_grub_mechanism(config, &mut rx); let _ = tokio::join!(config_fut, grub_fut); diff --git a/crates/api-grub/src/net.rs b/crates/api-grub/src/net.rs index a83a515..094c182 100644 --- a/crates/api-grub/src/net.rs +++ b/crates/api-grub/src/net.rs @@ -2,14 +2,39 @@ use anyhow::Result; use integr_structs::api::ApiConfig; use log::info; +use tokio::sync::mpsc::Receiver; +// use reqwest::Client; +struct ApiPoll<'a> { + config : &'a mut ApiConfig, + // client : Client, +} + +impl<'a> ApiPoll<'a> { + pub async fn new(poll_cfg : &'a mut ApiConfig) -> Self { + Self { + config : poll_cfg, + // client : Client::new(), + } + } +} // for api info pulling -pub async fn init_api_grub_mechanism(_config: &ApiConfig) -> Result<()> { +pub async fn init_api_grub_mechanism(config: ApiConfig, rx: &mut Receiver) -> Result<()> { info!("Initializing API-info grubbing mechanism..."); + + let mut config = config; + loop { + match config.endpoints.len() { + 0 => todo!(), + _ => todo!(), + } + } 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 diff --git a/crates/integr-structs/src/api.rs b/crates/integr-structs/src/api.rs index ec472ba..70f07a4 100644 --- a/crates/integr-structs/src/api.rs +++ b/crates/integr-structs/src/api.rs @@ -4,14 +4,14 @@ use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize, Debug)] pub struct ApiConfig { #[serde(default)] - endpoints : Vec, - delay : u32, + pub endpoints : Vec, + pub delay : u32, } #[derive(Serialize, Deserialize, Debug)] pub struct ApiEndpoint { - url : String, - method : String, + pub url : String, + pub method : String, } impl Default for ApiConfig {