improve proxy functionality and configuration
This commit is contained in:
67
src/config/docker.rs
Normal file
67
src/config/docker.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use crate::config::utils::generate_toml_parser_error_in_field;
|
||||
use std::{error::Error, fs::read_to_string};
|
||||
use toml::Table;
|
||||
|
||||
pub enum DockerConnectionKind {
|
||||
Http,
|
||||
HttpDefault,
|
||||
Unix,
|
||||
UnixDefault,
|
||||
}
|
||||
|
||||
pub struct DockerConnectionConfig {
|
||||
pub connection_kind: DockerConnectionKind,
|
||||
pub connection_string: Option<String>,
|
||||
pub network_name: String,
|
||||
pub network_addr: String,
|
||||
}
|
||||
|
||||
impl DockerConnectionConfig {
|
||||
pub fn get_config(file_name: &str) -> Result<Self, Box<dyn Error>> {
|
||||
let stored_file = read_to_string(file_name)?.parse::<Table>()?;
|
||||
let connection_kind_string = stored_file["docker"]["connection"].as_str().ok_or(
|
||||
generate_toml_parser_error_in_field("docker connection kind"),
|
||||
)?;
|
||||
|
||||
let connection_kind = match connection_kind_string {
|
||||
"http" => DockerConnectionKind::Http,
|
||||
"http_default" => DockerConnectionKind::HttpDefault,
|
||||
"unix" => DockerConnectionKind::Unix,
|
||||
"unix_default" => DockerConnectionKind::UnixDefault,
|
||||
_ => {
|
||||
return Err(Box::new(generate_toml_parser_error_in_field(
|
||||
"docker connection kind",
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
let connection_string = match connection_kind {
|
||||
DockerConnectionKind::Http | DockerConnectionKind::Unix => Some(
|
||||
stored_file["docker"]["string"]
|
||||
.as_str()
|
||||
.ok_or(generate_toml_parser_error_in_field(
|
||||
"docker connection string",
|
||||
))?
|
||||
.to_string(),
|
||||
),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let network_name = stored_file["docker"]["network_name"]
|
||||
.as_str()
|
||||
.ok_or(generate_toml_parser_error_in_field("docker network name"))?
|
||||
.to_string();
|
||||
|
||||
let network_addr = stored_file["docker"]["network_addr"]
|
||||
.as_str()
|
||||
.ok_or(generate_toml_parser_error_in_field("docker network addr"))?
|
||||
.to_string();
|
||||
|
||||
Ok(Self {
|
||||
connection_kind,
|
||||
connection_string,
|
||||
network_name,
|
||||
network_addr,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,102 +1,6 @@
|
||||
use std::{error::Error, fs::read_to_string, io};
|
||||
use toml::Table;
|
||||
pub mod docker;
|
||||
pub mod mrproxy;
|
||||
pub mod server;
|
||||
pub(in crate::config) mod utils;
|
||||
|
||||
pub const CONFIG_PATH: &str = "config/config.tom";
|
||||
|
||||
enum DockerConnectionKind {
|
||||
HTTP,
|
||||
HTTP_DEFAULT,
|
||||
UNIX,
|
||||
UNIX_DEFAULT,
|
||||
}
|
||||
|
||||
pub enum MrproxyConnectionKind {
|
||||
TCP,
|
||||
UNIX,
|
||||
}
|
||||
|
||||
pub struct DockerConnectionConfig {
|
||||
connection_kind: DockerConnectionKind,
|
||||
connection_string: Option<String>,
|
||||
}
|
||||
|
||||
pub struct MrproxyConnectionData {
|
||||
pub connection_kind: MrproxyConnectionKind,
|
||||
pub connection_string: String,
|
||||
}
|
||||
|
||||
impl DockerConnectionConfig {
|
||||
pub fn get_config(file_name: &str) -> Result<Self, Box<dyn Error>> {
|
||||
let stored_file = read_to_string(file_name)?.parse::<Table>()?;
|
||||
let connection_kind_string = stored_file["docker"]["connection"].as_str().ok_or(
|
||||
generate_toml_parser_error_in_field("docker connection kind"),
|
||||
)?;
|
||||
|
||||
let connection_kind = match connection_kind_string {
|
||||
"http" => DockerConnectionKind::HTTP,
|
||||
"http_default" => DockerConnectionKind::HTTP_DEFAULT,
|
||||
"unix" => DockerConnectionKind::UNIX,
|
||||
"unix_default" => DockerConnectionKind::UNIX_DEFAULT,
|
||||
_ => {
|
||||
return Err(Box::new(generate_toml_parser_error_in_field(
|
||||
"docker connection kind",
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
let connection_string = match connection_kind {
|
||||
DockerConnectionKind::HTTP | DockerConnectionKind::UNIX => Some(
|
||||
stored_file["docker"]["string"]
|
||||
.as_str()
|
||||
.ok_or(generate_toml_parser_error_in_field(
|
||||
"docker connection string",
|
||||
))?
|
||||
.to_string(),
|
||||
),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
connection_kind,
|
||||
connection_string,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl MrproxyConnectionData {
|
||||
pub fn get_config(file_name: &str) -> Result<Self, Box<dyn Error>> {
|
||||
let stored_file = read_to_string(file_name)?.parse::<Table>()?;
|
||||
let connection_kind_string = stored_file["mrproxy"]["connection"].as_str().ok_or(
|
||||
generate_toml_parser_error_in_field("mrproxy connection kind"),
|
||||
)?;
|
||||
|
||||
let connection_kind = match connection_kind_string {
|
||||
"tcp" => MrproxyConnectionKind::TCP,
|
||||
"unix" => MrproxyConnectionKind::UNIX,
|
||||
_ => {
|
||||
return Err(Box::new(generate_toml_parser_error_in_field(
|
||||
"mrproxy connection kind",
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
let connection_string = stored_file["mrproxy"]["string"]
|
||||
.as_str()
|
||||
.ok_or(generate_toml_parser_error_in_field(
|
||||
"mrproxy connection string",
|
||||
))?
|
||||
.to_string();
|
||||
|
||||
Ok(Self {
|
||||
connection_kind,
|
||||
connection_string,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_toml_parser_error_in_field(field: &str) -> io::Error {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Invalid format for config.toml in {} field", field),
|
||||
)
|
||||
}
|
||||
pub const CONFIG_PATH: &str = "config/config.toml";
|
||||
|
||||
44
src/config/mrproxy.rs
Normal file
44
src/config/mrproxy.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use crate::config::utils::generate_toml_parser_error_in_field;
|
||||
use std::{error::Error, fs::read_to_string};
|
||||
use toml::Table;
|
||||
|
||||
pub enum MrproxyConnectionKind {
|
||||
TCP,
|
||||
UNIX,
|
||||
}
|
||||
|
||||
pub struct MrproxyConnectionConfig {
|
||||
pub connection_kind: MrproxyConnectionKind,
|
||||
pub connection_string: String,
|
||||
}
|
||||
|
||||
impl MrproxyConnectionConfig {
|
||||
pub fn get_config(file_name: &str) -> Result<Self, Box<dyn Error>> {
|
||||
let stored_file = read_to_string(file_name)?.parse::<Table>()?;
|
||||
let connection_kind_string = stored_file["mrproxy"]["connection"].as_str().ok_or(
|
||||
generate_toml_parser_error_in_field("mrproxy connection kind"),
|
||||
)?;
|
||||
|
||||
let connection_kind = match connection_kind_string {
|
||||
"tcp" => MrproxyConnectionKind::TCP,
|
||||
"unix" => MrproxyConnectionKind::UNIX,
|
||||
_ => {
|
||||
return Err(Box::new(generate_toml_parser_error_in_field(
|
||||
"mrproxy connection kind",
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
let connection_string = stored_file["mrproxy"]["string"]
|
||||
.as_str()
|
||||
.ok_or(generate_toml_parser_error_in_field(
|
||||
"mrproxy connection string",
|
||||
))?
|
||||
.to_string();
|
||||
|
||||
Ok(Self {
|
||||
connection_kind,
|
||||
connection_string,
|
||||
})
|
||||
}
|
||||
}
|
||||
24
src/config/server.rs
Normal file
24
src/config/server.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::config::utils::generate_toml_parser_error_in_field;
|
||||
use std::{error::Error, fs::read_to_string};
|
||||
use toml::Table;
|
||||
|
||||
pub struct ServerConfig {
|
||||
pub addr: String,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl ServerConfig {
|
||||
pub fn get_config(file_name: &str) -> Result<Self, Box<dyn Error>> {
|
||||
let stored_file = read_to_string(file_name)?.parse::<Table>()?;
|
||||
let addr = stored_file["server"]["addr"]
|
||||
.as_str()
|
||||
.ok_or(generate_toml_parser_error_in_field("server addr"))?
|
||||
.to_string();
|
||||
|
||||
let port = stored_file["server"]["port"]
|
||||
.as_integer()
|
||||
.ok_or(generate_toml_parser_error_in_field("server port"))? as u16;
|
||||
|
||||
Ok(Self { addr, port })
|
||||
}
|
||||
}
|
||||
8
src/config/utils.rs
Normal file
8
src/config/utils.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use std::io;
|
||||
|
||||
pub fn generate_toml_parser_error_in_field(field: &str) -> io::Error {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Invalid format for config.toml in {} field", field),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user