reqwest added and a bit lines of code

pull/3/head
prplV 2025-01-16 17:26:33 +03:00
parent 28196ab06e
commit d8fcf19322
5 changed files with 42 additions and 15 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "api-grub" name = "api-grub"
version = "0.3.8" version = "0.3.13"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
@ -12,3 +12,4 @@ env_logger = "0.11.6"
log = "0.4.25" log = "0.4.25"
anyhow = "1.0.95" anyhow = "1.0.95"
chrono = "0.4.39" chrono = "0.4.39"
reqwest = { version = "0.12.12", features = ["rustls-tls", "json"] }

View File

@ -9,6 +9,7 @@ use serde_json::from_str;
use tokio::{io::AsyncReadExt, net::UnixListener}; use tokio::{io::AsyncReadExt, net::UnixListener};
use tokio::time::{sleep, Duration}; use tokio::time::{sleep, Duration};
use std::result::Result::Ok as stdOk; use std::result::Result::Ok as stdOk;
use tokio::sync::mpsc::Sender;
const CONFIG_PATH: &str = "config_api.json"; const CONFIG_PATH: &str = "config_api.json";
const SOCKET_PATH: &str = "api-grub.sock"; const SOCKET_PATH: &str = "api-grub.sock";
@ -38,7 +39,7 @@ pub async fn pull_local_config() -> Result<ApiConfig> {
// for config pulling // for config pulling
// ++++ reader to channel // ++++ reader to channel
pub async fn init_config_grub_mechanism() -> Result<()> { pub async fn init_config_grub_mechanism(tx: &Sender<ApiConfig>) -> Result<()> {
info!("Initializing Unix-Socket listening for pulling new configs..."); info!("Initializing Unix-Socket listening for pulling new configs...");
let server = init_unix_listener().await?; 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); warn!("Cannot read config from stream due to {}", er);
} else { } else {
let config: Result<ApiConfig, serde_json::Error> = from_str(&buffer); let config: Result<ApiConfig, serde_json::Error> = from_str(&buffer);
if let stdOk(_conf) = config { if let stdOk(conf) = config {
info!("New config was pulled from Unix-Stream. Saving it locally..."); 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 { if let Err(er) = save_new_config(&buffer).await {
error!("Cannot save new config locally due to: {}", er); error!("Cannot save new config locally due to: {}", er);
} }
// TODO! let _ = tx.send(conf).await;
// reading new config to channel
// saving new config locally
} else if let Err(er) = config { } else if let Err(er) = config {
warn!("Invalid config was pulled. Error: {}", er); warn!("Invalid config was pulled. Error: {}", er);
} }

View File

@ -8,6 +8,7 @@ use logger::setup_logger;
use log::{info, warn}; use log::{info, warn};
use config::{pull_local_config, init_config_grub_mechanism}; use config::{pull_local_config, init_config_grub_mechanism};
use net::init_api_grub_mechanism; use net::init_api_grub_mechanism;
use tokio::sync::mpsc;
#[tokio::main(flavor = "multi_thread")] #[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()>{ async fn main() -> Result<()>{
@ -17,10 +18,11 @@ async fn main() -> Result<()>{
// 3) ? // 3) ?
setup_logger().await?; setup_logger().await?;
let config = get_config().await; let config = get_config().await;
// config update channel
let (tx, mut rx) = mpsc::channel::<ApiConfig>(1);
// futures // futures
let config_fut = init_config_grub_mechanism(); let config_fut = init_config_grub_mechanism(&tx);
let grub_fut = init_api_grub_mechanism(&config); let grub_fut = init_api_grub_mechanism(config, &mut rx);
let _ = tokio::join!(config_fut, grub_fut); let _ = tokio::join!(config_fut, grub_fut);

View File

@ -2,14 +2,39 @@
use anyhow::Result; use anyhow::Result;
use integr_structs::api::ApiConfig; use integr_structs::api::ApiConfig;
use log::info; 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 // 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<ApiConfig>) -> Result<()> {
info!("Initializing API-info grubbing mechanism..."); info!("Initializing API-info grubbing mechanism...");
let mut config = config;
loop {
match config.endpoints.len() {
0 => todo!(),
_ => todo!(),
}
}
Ok(()) Ok(())
} }
// one-time exec func to send request, deserialize it and return object // one-time exec func to send request, deserialize it and return object
#[allow(dead_code)] #[allow(dead_code)]
async fn send_api_request() -> Result<()> {Ok(())} async fn send_api_request() -> Result<()> {Ok(())}

View File

@ -4,14 +4,14 @@ use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ApiConfig { pub struct ApiConfig {
#[serde(default)] #[serde(default)]
endpoints : Vec<ApiEndpoint>, pub endpoints : Vec<ApiEndpoint>,
delay : u32, pub delay : u32,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ApiEndpoint { pub struct ApiEndpoint {
url : String, pub url : String,
method : String, pub method : String,
} }
impl Default for ApiConfig { impl Default for ApiConfig {