From 4592c945862ffc8dd6a1646bad13968a5e0cb094 Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Fri, 23 Feb 2024 23:45:20 +0100 Subject: [PATCH] Reintroduce L4P_CONFIG environment variable This points to a user-configured configuration file. Closes #5. Signed-off-by: Jacob Kiers --- src/config/config_v1.rs | 5 +++-- src/main.rs | 30 +++++++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/config/config_v1.rs b/src/config/config_v1.rs index aeb3bad..123c4e2 100644 --- a/src/config/config_v1.rs +++ b/src/config/config_v1.rs @@ -1,6 +1,6 @@ use crate::upstreams::ProxyToUpstream; use crate::upstreams::Upstream; -use log::{debug, warn}; +use log::{debug, info, warn}; use serde::Deserialize; use std::collections::{HashMap, HashSet}; use std::fs::File; @@ -119,9 +119,10 @@ fn load_config(path: &str) -> Result { if !log_level.eq("disable") { std::env::set_var("FOURTH_LOG", log_level.clone()); pretty_env_logger::init_custom_env("FOURTH_LOG"); - debug!("Set log level to {}", log_level); } + info!("Using config file: {}", &path); + debug!("Set log level to {}", log_level); debug!("Config version {}", base.version); let mut parsed_upstream: HashMap = HashMap::new(); diff --git a/src/main.rs b/src/main.rs index 6372c4d..6c39046 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ mod upstreams; use crate::config::ConfigV1; use crate::servers::Server; -use log::{debug, error}; +use log::{debug, error, info}; use std::path::PathBuf; fn main() { @@ -37,20 +37,28 @@ fn main() { } fn find_config() -> Result> { - let possible_paths = ["/etc/l4p", ""]; + let possible_locations = ["/etc/l4p", ""]; let possible_names = ["l4p.yaml", "config.yaml"]; let mut tried_paths = Vec::::new(); + let mut possible_paths = Vec::::new(); - for path in possible_paths - .iter() - .flat_map(|&path| { - possible_names - .iter() - .map(move |&file| PathBuf::new().join(path).join(file)) - }) - .collect::>() - { + if let Ok(env_path) = std::env::var("L4P_CONFIG") { + possible_paths.push(PathBuf::from(env_path)); + } + + possible_paths.append( + &mut possible_locations + .iter() + .flat_map(|&path| { + possible_names + .iter() + .map(move |&file| PathBuf::new().join(path).join(file)) + }) + .collect::>(), + ); + + for path in possible_paths { let path_str = path.to_string_lossy().to_string(); if path.exists() { return Ok(path_str);