From 72ed870538f60fd53cc48d27536a4be7c24decf6 Mon Sep 17 00:00:00 2001 From: Ara Sadoyan Date: Fri, 27 Mar 2026 19:24:30 +0100 Subject: [PATCH] split upstreams.yaml file --- src/utils/parceyaml.rs | 47 ++++++++++++++++++++++++++++++++++++------ src/utils/structs.rs | 2 ++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/utils/parceyaml.rs b/src/utils/parceyaml.rs index 80bd579..473571b 100644 --- a/src/utils/parceyaml.rs +++ b/src/utils/parceyaml.rs @@ -5,14 +5,39 @@ use crate::utils::tools::{clone_dashmap, clone_dashmap_into, print_upstreams}; use dashmap::DashMap; use log::{error, info, warn}; use std::collections::HashMap; +use std::path::Path; use std::sync::atomic::AtomicUsize; use std::sync::Arc; use std::{env, fs}; pub async fn load_configuration(d: &str, kind: &str) -> (Option, String) { + let mut conf_files = Vec::new(); let yaml_data = match kind { "filepath" => match fs::read_to_string(d) { Ok(data) => { + let mut confdir = Path::new(d).parent().unwrap().to_path_buf(); + confdir.push("conf.d"); + if let Ok(entries) = fs::read_dir(&confdir) { + let mut paths: Vec<_> = entries + .flatten() + .map(|e| e.path()) + .filter(|p| p.extension().and_then(|e| e.to_str()) == Some("yaml")) + .collect(); + paths.sort(); + + for path in paths { + let content = fs::read_to_string(&path); + match content { + Ok(content) => { + conf_files.push(content); + } + Err(e) => { + error!("Reading: {}: {:?}", path.display(), e) + } + }; + } + } + info!("Reading upstreams from {}", d); data } @@ -32,18 +57,28 @@ pub async fn load_configuration(d: &str, kind: &str) -> (Option, } }; - let parsed: Config = match serde_yaml::from_str(&yaml_data) { - Ok(cfg) => { - // println!("{:#?}", cfg); - cfg - } + let mut parsed: Config = match serde_yaml::from_str(&yaml_data) { + Ok(cfg) => cfg, Err(e) => { error!("Failed to parse upstreams file: {}", e); return (None, e.to_string()); } }; - let mut toreturn = Configuration::default(); + if let Some(ref mut upstreams) = parsed.upstreams { + for uconf in conf_files { + let p: HashMap = match serde_yaml::from_str(&uconf) { + Ok(ucfg) => ucfg, + Err(e) => { + error!("Failed to parse upstreams file: {}", e); + return (None, e.to_string()); + } + }; + upstreams.extend(p); + } + } + + let mut toreturn = Configuration::default(); populate_headers_and_auth(&mut toreturn, &parsed).await; toreturn.typecfg = parsed.provider.clone(); diff --git a/src/utils/structs.rs b/src/utils/structs.rs index 1795bf2..161cdc2 100644 --- a/src/utils/structs.rs +++ b/src/utils/structs.rs @@ -8,6 +8,8 @@ pub type UpstreamsDashMap = DashMap, DashMap, (Vec>; pub type Headers = DashMap, DashMap, Vec<(String, Arc)>>>; +// pub type UpstreamsSerDde = Option>; +// pub type UpstreamsSerDe = HashMap; #[derive(Clone, Debug, Default)] pub struct Extraparams {