Intermediate commit

This commit is contained in:
Ara Sadoyan
2025-03-10 09:31:22 +01:00
parent a109b027e1
commit a10a08678b
3 changed files with 101 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ use std::fs;
use std::sync::atomic::AtomicUsize;
use std::time::{Duration, Instant};
use crate::utils::tools::*;
use crate::web::webserver;
use async_trait::async_trait;
use notify::event::ModifyKind;
@@ -76,6 +77,17 @@ pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(S
start = Instant::now();
println!("Config File changed :=> {:?}", e);
let _sd = build_upstreams2("etc/upstreams-long.conf", "filepath");
println!("\n\n");
for t in _sd.iter() {
println!("{} ==>", t.key());
for v in t.value().iter() {
println!(" {:?}", v)
}
}
println!("\n\n");
let snd = build_upstreams(file_path, "filepath");
let _ = toreturn.send(snd).await.unwrap();
}
@@ -137,3 +149,66 @@ pub fn build_upstreams(d: &str, kind: &str) -> DashMap<String, (Vec<(String, u16
upstreams
}
pub fn build_upstreams2(d: &str, kind: &str) -> DashMap<String, Vec<UpstreamsStruct>> {
let upstreams: DashMap<String, Vec<UpstreamsStruct>> = DashMap::new();
let mut contents = d.to_string();
match kind {
"filepath" => {
println!("Reading upstreams from {}", d);
let _ = match fs::read_to_string(d) {
Ok(data) => contents = data,
Err(e) => {
eprintln!("Error reading file: {:?}", e);
return upstreams;
}
};
}
"content" => {
println!("Reading upstreams from API post body");
}
_ => println!("*******************> nothing <*******************"),
}
for line in contents.lines().filter(|line| !line.trim().is_empty()) {
let mut parts = line.split_whitespace();
let Some(hostname) = parts.next() else {
continue;
};
let Some(ssl) = crate::utils::tools::string_to_bool(parts.next()) else {
continue;
};
let Some(proto) = parts.next() else {
continue;
};
let Some(path) = parts.next() else {
continue;
};
let Some(address) = parts.next() else {
continue;
};
let mut addr_parts = address.split(':');
let Some(ip) = addr_parts.next() else {
continue;
};
let Some(port_str) = addr_parts.next() else {
continue;
};
let Ok(port) = port_str.parse::<u16>() else {
continue;
};
let d = UpstreamsStruct {
proto: proto.to_string(),
path: path.to_string(),
address: (ip.to_string(), port, ssl),
atom: AtomicUsize::new(0),
};
upstreams.entry(hostname.to_string()).or_insert_with(|| Vec::new()).push(d);
}
upstreams
}

View File

@@ -1,7 +1,28 @@
use std::any::type_name;
use std::sync::atomic::AtomicUsize;
#[derive(Debug)]
#[allow(dead_code)]
pub struct UpstreamsStruct {
pub proto: String,
pub path: String,
pub address: (String, u16, bool),
pub atom: AtomicUsize,
}
#[allow(dead_code)]
pub fn typeoff<T>(_: T) {
let to = type_name::<T>();
println!("{:?}", to);
}
pub fn string_to_bool(val: Option<&str>) -> Option<bool> {
match val {
Some(v) => match v {
"yes" => Some(true),
"true" => Some(true),
_ => Some(false),
},
None => Some(false),
}
}

View File

@@ -123,13 +123,13 @@ impl ProxyHttp for LB {
let ddr = self.get_host(h[0]);
match ddr.await {
Some((host, port)) => {
let peer = Box::new(HttpPeer::new((host, port), false, "".to_string()));
let peer = Box::new(HttpPeer::new((host, port), false, String::new()));
info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed());
Ok(peer)
}
None => {
warn!("Returning default list => {:?}", ("127.0.0.1", 3000));
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, "".to_string()));
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new()));
info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed());
Ok(peer)
}
@@ -137,7 +137,7 @@ impl ProxyHttp for LB {
}
None => {
warn!("Returning default list => {:?}", ("127.0.0.1", 3000));
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, "".to_string()));
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new()));
info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed());
Ok(peer)
}
@@ -146,12 +146,12 @@ impl ProxyHttp for LB {
let ddr = self.get_host(host_name.unwrap().to_str().unwrap());
match ddr.await {
Some((host, port)) => {
let peer = Box::new(HttpPeer::new((host, port), false, "".to_string()));
let peer = Box::new(HttpPeer::new((host, port), false, String::new()));
Ok(peer)
}
None => {
println!("Returning default list => {:?}", ("127.0.0.1", 3000));
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, "".to_string()));
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new()));
Ok(peer)
}
}