diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index 0605075..0000000 --- a/src/config.rs +++ /dev/null @@ -1,174 +0,0 @@ -use serde_yaml; -use std::error::Error; -use std::fmt; -use std::fs::File; -use std::io::prelude::*; - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct ServerConfig { - pub host: Option, - pub port: Option, - pub workers: Option -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct CloudflareDomainConfig { - pub domain: String, - pub zone_id: String -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct CloudflareConfig { - pub domains: Vec, - pub key: String, - pub email: String -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct DDNSDomain { - pub domain: String, - pub subdomains: Vec -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct DDNSConfig { - pub domains: Vec -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct UserSubdomainConfig { - subdomain: String, - targets: Vec -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct UserDomainConfig { - pub domain: String, - pub subdomains: Vec -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct UserConfig { - pub username: String, - pub token: String, - pub domains: Vec -} - - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct Config { - pub server: ServerConfig, - pub cloudflare: CloudflareConfig, - pub ddns: DDNSConfig, - pub users: Vec -} - -fn get_default_config() -> Config { - Config { - server: ServerConfig { - host: Option::Some(String::from("localhost")), - port: Option::Some(8080), - workers: Option::Some(4) - }, - cloudflare: CloudflareConfig { - email: String::from("something@something.com"), - key: String::from("IAmNotAKey"), - domains: vec![ - CloudflareDomainConfig { - domain: String::from("IAmNotADomain.com"), - zone_id: String::from("IAmNotAZoneID") - } - ] - }, - ddns: DDNSConfig { - domains: vec![ - DDNSDomain { - domain: String::from("IAmNotADomain.com"), - subdomains: vec![ - String::from("ddns") - ] - } - ] - }, - users: vec![ - UserConfig { - username: String::from("userOne"), - token: String::from("6d37d7a9-6b6b-4db2-99f2-c261e4f4b922"), - domains: vec![ - UserDomainConfig { - domain: String::from("IAmNotADomain.com"), - subdomains: vec![ - UserSubdomainConfig { - subdomain: String::from("ddns"), - targets: vec![ - String::from("home") - ] - } - ] - } - ] - } - ] - } -} - -#[derive(Debug)] -pub struct ConfigError { - description: String, - original_error: Option> -} - -impl ConfigError { - fn new(description: &str, original_error: Option>) -> ConfigError { - ConfigError { - description: String::from(description), - original_error - } - } -} - -impl Error for ConfigError {} - -impl fmt::Display for ConfigError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match &self.original_error { - Some(original_error) => { - write!(f, "{}: \"{}\"", self.description, original_error) - }, - None => write!(f, "{}", self.description) - } - } -} - -fn read_config(yaml_str: &str) -> Result { - match serde_yaml::from_str(yaml_str) { - Ok(v) => Result::Ok(v), - Err(e) => Result::Err(ConfigError::new("Invalid Configuration", Option::Some(Box::from(e)))) - } -} - -pub fn load_config(path: &str) -> Result { - match File::open(path) { - Ok(mut file) => { - let mut contents = String::new(); - match file.read_to_string(&mut contents) { - Ok(c) => { - if c > 0 { - read_config(&contents) - } else { - Result::Err(ConfigError::new("Empty Configuration File", Option::None)) - } - }, - Err(e) => Result::Err(ConfigError::new("Cannot Read Configuration File", Option::Some(Box::from(e)))) - } - }, - Err(_e) => Result::Err(ConfigError::new(&format!("Configuration File Doesn't Exist \"{}\"", path), Option::None)) - } -} \ No newline at end of file diff --git a/src/config/default.rs b/src/config/default.rs new file mode 100644 index 0000000..5b8901c --- /dev/null +++ b/src/config/default.rs @@ -0,0 +1,50 @@ +use config::model::*; + +fn get_default_config() -> Config { + Config { + server: ServerConfig { + host: Option::Some(String::from("localhost")), + port: Option::Some(8080), + workers: Option::Some(4) + }, + cloudflare: CloudflareConfig { + email: String::from("something@something.com"), + key: String::from("IAmNotAKey"), + domains: vec![ + CloudflareDomainConfig { + domain: String::from("IAmNotADomain.com"), + zone_id: String::from("IAmNotAZoneID") + } + ] + }, + ddns: DDNSConfig { + domains: vec![ + DDNSDomain { + domain: String::from("IAmNotADomain.com"), + subdomains: vec![ + String::from("ddns") + ] + } + ] + }, + users: vec![ + UserConfig { + username: String::from("userOne"), + token: String::from("6d37d7a9-6b6b-4db2-99f2-c261e4f4b922"), + domains: vec![ + UserDomainConfig { + domain: String::from("IAmNotADomain.com"), + subdomains: vec![ + UserSubdomainConfig { + subdomain: String::from("ddns"), + targets: vec![ + String::from("home") + ] + } + ] + } + ] + } + ] + } +} \ No newline at end of file diff --git a/src/config/error.rs b/src/config/error.rs new file mode 100644 index 0000000..0d627a7 --- /dev/null +++ b/src/config/error.rs @@ -0,0 +1,30 @@ +use std::error::Error; +use std::fmt; + +#[derive(Debug)] +pub struct ConfigError { + description: String, + original_error: Option> +} + +impl ConfigError { + pub fn new(description: &str, original_error: Option>) -> ConfigError { + ConfigError { + description: String::from(description), + original_error + } + } +} + +impl Error for ConfigError {} + +impl fmt::Display for ConfigError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match &self.original_error { + Some(original_error) => { + write!(f, "{}: \"{}\"", self.description, original_error) + }, + None => write!(f, "{}", self.description) + } + } +} \ No newline at end of file diff --git a/src/config/load.rs b/src/config/load.rs new file mode 100644 index 0000000..3c9c2d2 --- /dev/null +++ b/src/config/load.rs @@ -0,0 +1,32 @@ +use serde_yaml; +use std::fs::File; +use std::io::prelude::*; + +use config::error::ConfigError; +use config::model::Config; + +fn read_config(yaml_str: &str) -> Result { + match serde_yaml::from_str(yaml_str) { + Ok(v) => Result::Ok(v), + Err(e) => Result::Err(ConfigError::new("Invalid Configuration", Option::Some(Box::from(e)))) + } +} + +pub fn read(path: &str) -> Result { + match File::open(path) { + Ok(mut file) => { + let mut contents = String::new(); + match file.read_to_string(&mut contents) { + Ok(c) => { + if c > 0 { + read_config(&contents) + } else { + Result::Err(ConfigError::new("Empty Configuration File", Option::None)) + } + }, + Err(e) => Result::Err(ConfigError::new("Cannot Read Configuration File", Option::Some(Box::from(e)))) + } + }, + Err(_e) => Result::Err(ConfigError::new(&format!("Configuration File Doesn't Exist \"{}\"", path), Option::None)) + } +} \ No newline at end of file diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..3d9d82b --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,4 @@ +pub mod default; +pub mod error; +pub mod load; +pub mod model; \ No newline at end of file diff --git a/src/config/model.rs b/src/config/model.rs new file mode 100644 index 0000000..9a6a098 --- /dev/null +++ b/src/config/model.rs @@ -0,0 +1,66 @@ + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct ServerConfig { + pub host: Option, + pub port: Option, + pub workers: Option +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct CloudflareDomainConfig { + pub domain: String, + pub zone_id: String +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct CloudflareConfig { + pub domains: Vec, + pub key: String, + pub email: String +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct DDNSDomain { + pub domain: String, + pub subdomains: Vec +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct DDNSConfig { + pub domains: Vec +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct UserSubdomainConfig { + pub subdomain: String, + pub targets: Vec +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct UserDomainConfig { + pub domain: String, + pub subdomains: Vec +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct UserConfig { + pub username: String, + pub token: String, + pub domains: Vec +} + + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct Config { + pub server: ServerConfig, + pub cloudflare: CloudflareConfig, + pub ddns: DDNSConfig, + pub users: Vec +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b2f4eba..a3e027d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,7 @@ fn main() { .get_matches(); let config_path: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml"); - let config = match config::load_config(config_path) { + let config = match config::load::read(config_path) { Ok(c) => { info!("Loaded configuration from \"{}\"", config_path); Option::Some(c)