Reintroduce L4P_CONFIG environment variable
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details

This points to a user-configured configuration file.

Closes #5.

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2024-02-23 23:45:20 +01:00
parent 6284870059
commit 4592c94586
2 changed files with 22 additions and 13 deletions

View File

@ -1,6 +1,6 @@
use crate::upstreams::ProxyToUpstream; use crate::upstreams::ProxyToUpstream;
use crate::upstreams::Upstream; use crate::upstreams::Upstream;
use log::{debug, warn}; use log::{debug, info, warn};
use serde::Deserialize; use serde::Deserialize;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs::File; use std::fs::File;
@ -119,9 +119,10 @@ fn load_config(path: &str) -> Result<ParsedConfigV1, ConfigError> {
if !log_level.eq("disable") { if !log_level.eq("disable") {
std::env::set_var("FOURTH_LOG", log_level.clone()); std::env::set_var("FOURTH_LOG", log_level.clone());
pretty_env_logger::init_custom_env("FOURTH_LOG"); 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); debug!("Config version {}", base.version);
let mut parsed_upstream: HashMap<String, Upstream> = HashMap::new(); let mut parsed_upstream: HashMap<String, Upstream> = HashMap::new();

View File

@ -5,7 +5,7 @@ mod upstreams;
use crate::config::ConfigV1; use crate::config::ConfigV1;
use crate::servers::Server; use crate::servers::Server;
use log::{debug, error}; use log::{debug, error, info};
use std::path::PathBuf; use std::path::PathBuf;
fn main() { fn main() {
@ -37,20 +37,28 @@ fn main() {
} }
fn find_config() -> Result<String, Vec<String>> { fn find_config() -> Result<String, Vec<String>> {
let possible_paths = ["/etc/l4p", ""]; let possible_locations = ["/etc/l4p", ""];
let possible_names = ["l4p.yaml", "config.yaml"]; let possible_names = ["l4p.yaml", "config.yaml"];
let mut tried_paths = Vec::<String>::new(); let mut tried_paths = Vec::<String>::new();
let mut possible_paths = Vec::<PathBuf>::new();
for path in possible_paths if let Ok(env_path) = std::env::var("L4P_CONFIG") {
.iter() possible_paths.push(PathBuf::from(env_path));
.flat_map(|&path| { }
possible_names
.iter() possible_paths.append(
.map(move |&file| PathBuf::new().join(path).join(file)) &mut possible_locations
}) .iter()
.collect::<Vec<PathBuf>>() .flat_map(|&path| {
{ possible_names
.iter()
.map(move |&file| PathBuf::new().join(path).join(file))
})
.collect::<Vec<PathBuf>>(),
);
for path in possible_paths {
let path_str = path.to_string_lossy().to_string(); let path_str = path.to_string_lossy().to_string();
if path.exists() { if path.exists() {
return Ok(path_str); return Ok(path_str);