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;
#[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<String>,
pub servers: HashMap<String, ServerConfig>,
@ -94,15 +94,15 @@ pub enum ConfigError {
Custom(String),
}
impl Config {
pub fn new(path: &str) -> Result<Config, ConfigError> {
impl ConfigV1 {
pub fn new(path: &str) -> Result<ConfigV1, ConfigError> {
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 file = File::open(path)?;
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));
}
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<ParsedConfig, ConfigError> {
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 upstream_names: HashSet<String> = HashSet::new();
let mut listen_addresses: HashSet<String> = 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);

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 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();

View File

@ -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();
});