mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 23:08:40 +08:00
temp remove command line arg parser
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use crate::utils::tools::*;
|
||||
use dashmap::DashMap;
|
||||
use log::{error, info, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml::Error;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
@@ -22,40 +24,63 @@ struct PathConfig {
|
||||
servers: Vec<String>,
|
||||
}
|
||||
|
||||
pub fn load_yaml_to_dashmap(d: &str, kind: &str) -> UpstreamsDashMap {
|
||||
pub fn load_yaml_to_dashmap(d: &str, kind: &str) -> Option<UpstreamsDashMap> {
|
||||
let dashmap = UpstreamsDashMap::new();
|
||||
let mut yaml_data = d.to_string();
|
||||
match kind {
|
||||
"filepath" => {
|
||||
println!("Reading upstreams from {}", d);
|
||||
let _ = match fs::read_to_string(d) {
|
||||
Ok(data) => yaml_data = data,
|
||||
Ok(data) => {
|
||||
info!("Reading upstreams from {}", d);
|
||||
yaml_data = data
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file: {:?}", e);
|
||||
return dashmap;
|
||||
error!("Reading: {}: {:?}", d, e.to_string());
|
||||
warn!("Running with empty upstreams list, chane it via API");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
}
|
||||
"content" => {
|
||||
println!("Reading upstreams from API post body");
|
||||
info!("Reading upstreams from API post body");
|
||||
}
|
||||
_ => println!("*******************> nothing <*******************"),
|
||||
_ => error!("Mismatched parameter, only filepath|content is allowed "),
|
||||
}
|
||||
let parsed: Config = serde_yaml::from_str(&yaml_data).expect("Failed to parse YAML");
|
||||
for (hostname, host_config) in parsed.upstreams {
|
||||
let path_map = DashMap::new();
|
||||
for (path, path_config) in host_config.paths {
|
||||
let mut server_list = Vec::new();
|
||||
for server in path_config.servers {
|
||||
if let Some((ip, port_str)) = server.split_once(':') {
|
||||
if let Ok(port) = port_str.parse::<u16>() {
|
||||
server_list.push((ip.to_string(), port, path_config.ssl, path_config.protocol.clone()));
|
||||
|
||||
let p: Result<Config, Error> = serde_yaml::from_str(&yaml_data);
|
||||
match p {
|
||||
Ok(parsed) => {
|
||||
for (hostname, host_config) in parsed.upstreams {
|
||||
let path_map = DashMap::new();
|
||||
for (path, path_config) in host_config.paths {
|
||||
let mut server_list = Vec::new();
|
||||
for server in path_config.servers {
|
||||
if let Some((ip, port_str)) = server.split_once(':') {
|
||||
if let Ok(port) = port_str.parse::<u16>() {
|
||||
server_list.push((ip.to_string(), port, path_config.ssl, path_config.protocol.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
path_map.insert(path, (server_list, AtomicUsize::new(0)));
|
||||
}
|
||||
dashmap.insert(hostname, path_map);
|
||||
}
|
||||
path_map.insert(path, (server_list, AtomicUsize::new(0)));
|
||||
Some(dashmap)
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to parse upstreams file: {}", e);
|
||||
None
|
||||
}
|
||||
dashmap.insert(hostname, path_map);
|
||||
}
|
||||
dashmap
|
||||
}
|
||||
|
||||
pub fn parce_main_config(path: &str) -> DashMap<String, String> {
|
||||
info!("Parsing configuration");
|
||||
let data = fs::read_to_string(path).unwrap();
|
||||
let reply = DashMap::new();
|
||||
let cfg: HashMap<String, String> = serde_yaml::from_str(&*data).expect("Failed to parse main config file");
|
||||
for (k, v) in cfg {
|
||||
reply.insert(k.to_string(), v.to_string());
|
||||
}
|
||||
reply
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user