From 17b39dc6bc6636d24bcbbfa7758064a5dc9d1f29 Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Thu, 5 Oct 2023 13:32:24 +0200 Subject: [PATCH] Prepare for new config version Signed-off-by: Jacob Kiers --- src/{ => config}/config.rs | 20 ++++++++++---------- src/config/mod.rs | 3 +++ src/main.rs | 6 +++--- src/servers/mod.rs | 10 +++++----- 4 files changed, 21 insertions(+), 18 deletions(-) rename src/{ => config}/config.rs (93%) create mode 100644 src/config/mod.rs diff --git a/src/config.rs b/src/config/config.rs similarity index 93% rename from src/config.rs rename to src/config/config.rs index 5d243bf..bba5334 100644 --- a/src/config.rs +++ b/src/config/config.rs @@ -8,12 +8,12 @@ use std::io::{Error as IOError, Read}; use url::Url; #[derive(Debug, Clone)] -pub struct Config { - pub base: ParsedConfig, +pub struct ConfigV1 { + pub base: ParsedConfigV1, } #[derive(Debug, Default, Deserialize, Clone)] -pub struct ParsedConfig { +pub struct ParsedConfigV1 { pub version: i32, pub log: Option, pub servers: HashMap, @@ -94,15 +94,15 @@ pub enum ConfigError { Custom(String), } -impl Config { - pub fn new(path: &str) -> Result { +impl ConfigV1 { + pub fn new(path: &str) -> Result { let base = load_config(path)?; - Ok(Config { base }) + Ok(ConfigV1 { base }) } } -fn load_config(path: &str) -> Result { +fn load_config(path: &str) -> Result { let mut contents = String::new(); let mut file = File::open(path)?; file.read_to_string(&mut contents)?; @@ -134,7 +134,7 @@ fn load_config(path: &str) -> Result { parsed_upstream.insert(name.to_string(), Upstream::Proxy(ups)); } - let parsed = ParsedConfig { + let parsed = ParsedConfigV1 { version: base.version, log: base.log, servers: base.servers, @@ -144,7 +144,7 @@ fn load_config(path: &str) -> Result { verify_config(parsed) } -fn verify_config(config: ParsedConfig) -> Result { +fn verify_config(config: ParsedConfigV1) -> Result { let mut used_upstreams: HashSet = HashSet::new(); let mut upstream_names: HashSet = HashSet::new(); let mut listen_addresses: HashSet = HashSet::new(); @@ -218,7 +218,7 @@ mod tests { #[test] fn test_load_config() { - let config = Config::new("tests/config.yaml").unwrap(); + let config = ConfigV1::new("tests/config.yaml").unwrap(); assert_eq!(config.base.version, 1); assert_eq!(config.base.log.unwrap(), "disable"); assert_eq!(config.base.servers.len(), 5); diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..8e1aad2 --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,3 @@ +mod config; +pub(crate) use config::ConfigV1; +pub(crate) use config::ParsedConfigV1; diff --git a/src/main.rs b/src/main.rs index 0b92f6c..0078874 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod plugins; mod servers; mod upstreams; -use crate::config::Config; +use crate::config::ConfigV1; use crate::servers::Server; use log::{debug, error}; @@ -13,7 +13,7 @@ use std::path::Path; fn main() { let config_path = find_config(); - let config = match Config::new(&config_path) { + let config = match ConfigV1::new(&config_path) { Ok(config) => config, Err(e) => { println!("Could not load config: {:?}", e); @@ -22,7 +22,7 @@ fn main() { }; debug!("{:?}", config); - let mut server = Server::new(config.base); + let mut server = Server::new_from_v1_config(config.base); debug!("{:?}", server); let _ = server.run(); diff --git a/src/servers/mod.rs b/src/servers/mod.rs index c90391e..837b577 100644 --- a/src/servers/mod.rs +++ b/src/servers/mod.rs @@ -8,7 +8,7 @@ use tokio::task::JoinHandle; mod protocol; pub(crate) mod upstream_address; -use crate::config::ParsedConfig; +use crate::config::ParsedConfigV1; use crate::upstreams::Upstream; use protocol::tcp; @@ -29,7 +29,7 @@ pub(crate) struct Proxy { } impl Server { - pub fn new(config: ParsedConfig) -> Self { + pub fn new_from_v1_config(config: ParsedConfigV1) -> Self { let mut new_server = Server { proxies: Vec::new(), }; @@ -155,9 +155,9 @@ mod tests { #[tokio::test] async fn test_proxy() { - use crate::config::Config; - let config = Config::new("tests/config.yaml").unwrap(); - let mut server = Server::new(config.base); + use crate::config::ConfigV1; + let config = ConfigV1::new("tests/config.yaml").unwrap(); + let mut server = Server::new_from_v1_config(config.base); thread::spawn(move || { tcp_mock_server(); });