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]
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"] }

View File

@ -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<ApiConfig> {
// for config pulling
// ++++ 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...");
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<ApiConfig, serde_json::Error> = 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);
}

View File

@ -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::<ApiConfig>(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);

View File

@ -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<ApiConfig>) -> 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(())}

View File

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