Prepare for new config version

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2023-10-05 13:32:24 +02:00
parent 07fccb6b2a
commit 17b39dc6bc
4 changed files with 21 additions and 18 deletions

View File

@ -8,12 +8,12 @@ use std::io::{Error as IOError, Read};
use url::Url; use url::Url;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Config { pub struct ConfigV1 {
pub base: ParsedConfig, pub base: ParsedConfigV1,
} }
#[derive(Debug, Default, Deserialize, Clone)] #[derive(Debug, Default, Deserialize, Clone)]
pub struct ParsedConfig { pub struct ParsedConfigV1 {
pub version: i32, pub version: i32,
pub log: Option<String>, pub log: Option<String>,
pub servers: HashMap<String, ServerConfig>, pub servers: HashMap<String, ServerConfig>,
@ -94,15 +94,15 @@ pub enum ConfigError {
Custom(String), Custom(String),
} }
impl Config { impl ConfigV1 {
pub fn new(path: &str) -> Result<Config, ConfigError> { pub fn new(path: &str) -> Result<ConfigV1, ConfigError> {
let base = load_config(path)?; let base = load_config(path)?;
Ok(Config { base }) Ok(ConfigV1 { base })
} }
} }
fn load_config(path: &str) -> Result<ParsedConfig, ConfigError> { fn load_config(path: &str) -> Result<ParsedConfigV1, ConfigError> {
let mut contents = String::new(); let mut contents = String::new();
let mut file = File::open(path)?; let mut file = File::open(path)?;
file.read_to_string(&mut contents)?; file.read_to_string(&mut contents)?;
@ -134,7 +134,7 @@ fn load_config(path: &str) -> Result<ParsedConfig, ConfigError> {
parsed_upstream.insert(name.to_string(), Upstream::Proxy(ups)); parsed_upstream.insert(name.to_string(), Upstream::Proxy(ups));
} }
let parsed = ParsedConfig { let parsed = ParsedConfigV1 {
version: base.version, version: base.version,
log: base.log, log: base.log,
servers: base.servers, servers: base.servers,
@ -144,7 +144,7 @@ fn load_config(path: &str) -> Result<ParsedConfig, ConfigError> {
verify_config(parsed) verify_config(parsed)
} }
fn verify_config(config: ParsedConfig) -> Result<ParsedConfig, ConfigError> { fn verify_config(config: ParsedConfigV1) -> Result<ParsedConfigV1, ConfigError> {
let mut used_upstreams: HashSet<String> = HashSet::new(); let mut used_upstreams: HashSet<String> = HashSet::new();
let mut upstream_names: HashSet<String> = HashSet::new(); let mut upstream_names: HashSet<String> = HashSet::new();
let mut listen_addresses: HashSet<String> = HashSet::new(); let mut listen_addresses: HashSet<String> = HashSet::new();
@ -218,7 +218,7 @@ mod tests {
#[test] #[test]
fn test_load_config() { 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.version, 1);
assert_eq!(config.base.log.unwrap(), "disable"); assert_eq!(config.base.log.unwrap(), "disable");
assert_eq!(config.base.servers.len(), 5); assert_eq!(config.base.servers.len(), 5);

3
src/config/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod config;
pub(crate) use config::ConfigV1;
pub(crate) use config::ParsedConfigV1;

View File

@ -3,7 +3,7 @@ mod plugins;
mod servers; mod servers;
mod upstreams; mod upstreams;
use crate::config::Config; use crate::config::ConfigV1;
use crate::servers::Server; use crate::servers::Server;
use log::{debug, error}; use log::{debug, error};
@ -13,7 +13,7 @@ use std::path::Path;
fn main() { fn main() {
let config_path = find_config(); let config_path = find_config();
let config = match Config::new(&config_path) { let config = match ConfigV1::new(&config_path) {
Ok(config) => config, Ok(config) => config,
Err(e) => { Err(e) => {
println!("Could not load config: {:?}", e); println!("Could not load config: {:?}", e);
@ -22,7 +22,7 @@ fn main() {
}; };
debug!("{:?}", config); debug!("{:?}", config);
let mut server = Server::new(config.base); let mut server = Server::new_from_v1_config(config.base);
debug!("{:?}", server); debug!("{:?}", server);
let _ = server.run(); let _ = server.run();

View File

@ -8,7 +8,7 @@ use tokio::task::JoinHandle;
mod protocol; mod protocol;
pub(crate) mod upstream_address; pub(crate) mod upstream_address;
use crate::config::ParsedConfig; use crate::config::ParsedConfigV1;
use crate::upstreams::Upstream; use crate::upstreams::Upstream;
use protocol::tcp; use protocol::tcp;
@ -29,7 +29,7 @@ pub(crate) struct Proxy {
} }
impl Server { impl Server {
pub fn new(config: ParsedConfig) -> Self { pub fn new_from_v1_config(config: ParsedConfigV1) -> Self {
let mut new_server = Server { let mut new_server = Server {
proxies: Vec::new(), proxies: Vec::new(),
}; };
@ -155,9 +155,9 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_proxy() { async fn test_proxy() {
use crate::config::Config; use crate::config::ConfigV1;
let config = Config::new("tests/config.yaml").unwrap(); let config = ConfigV1::new("tests/config.yaml").unwrap();
let mut server = Server::new(config.base); let mut server = Server::new_from_v1_config(config.base);
thread::spawn(move || { thread::spawn(move || {
tcp_mock_server(); tcp_mock_server();
}); });