Browse Source

Working on loading from config file

develop
Drew Short 6 years ago
parent
commit
667e713a18
  1. 1
      .gitignore
  2. 6
      .gitlab-ci.yml
  3. 16
      rsddns-example.yml
  4. 138
      src/config.rs
  5. 11
      src/main.rs

1
.gitignore

@ -1,2 +1,3 @@
/target
**/*.rs.bk
rsddns.yml

6
.gitlab-ci.yml

@ -4,7 +4,7 @@ stages:
- deploy
tests:
image: rust:1.29-stretch
image: rust:1.30-stretch
stage: test
script:
- cargo test
@ -12,7 +12,7 @@ tests:
- docker
debug:
image: rust:1.29-stretch
image: rust:1.30-stretch
stage: build
script:
- cargo build
@ -24,7 +24,7 @@ debug:
- docker
release:
image: rust:1.29-stretch
image: rust:1.30-stretch
stage: build
script:
- cargo build --release

16
rsddns-example.yml

@ -0,0 +1,16 @@
---
server:
host: localhost
port: 8080
workers: 4
cloudflare:
key: somekeyblahblahblahimakey
domains:
- domain: something.com
zone_id: blahblahchangemeimakey
ddns:
domains:
- domain: something.com
subdomains:
- ddns
---

138
src/config.rs

@ -0,0 +1,138 @@
use yaml_rust::{Yaml, YamlLoader};
use std::error::Error;
use std::fmt;
pub struct ServerConfig {
pub host: String,
pub port: i64,
pub workers: i64,
}
pub struct CloudflareDomainConfig {
pub domain: String,
pub zone_id: String,
}
pub struct CloudflareConfig {
pub domains: Vec<CloudflareDomainConfig>,
pub key: String,
}
pub struct DDNSDomain {
pub domain: String,
pub subdomains: Vec<String>,
}
pub struct DDNSConfig {
pub domains: Vec<DDNSDomain>
}
pub struct Config {
pub server: ServerConfig,
pub cloudflare: CloudflareConfig,
pub ddns: DDNSConfig,
}
fn get_default_config() -> Config {
Config {
server: ServerConfig {
host: String::from("localhost"),
port: 8080,
workers: 4
},
cloudflare: CloudflareConfig {
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")
]
}
]
}
}
}
#[derive(Debug)]
pub struct ConfigError;
impl Error for ConfigError {}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Configuration Error")
}
}
fn create_server_config_from_yaml(yaml: &Yaml) -> Result<ServerConfig, ConfigError> {
Result::Ok(ServerConfig {
host: String::from(yaml["host"].as_str().unwrap()),
port: yaml["port"].as_i64().unwrap(),
workers: yaml["workers"].as_i64().unwrap(),
})
}
fn create_cloudflare_domain_config_from_yaml(yaml: &Yaml) -> Result<CloudflareDomainConfig, ConfigError> {
Result::Ok(CloudflareDomainConfig {
domain: String::from(yaml["domain"].as_str().unwrap()),
zone_id: String::from(yaml["zone_id"].as_str().unwrap()),
})
}
fn create_cloudflare_config_from_yaml(yaml: &Yaml) -> Result<CloudflareConfig, ConfigError> {
Result::Ok(CloudflareConfig {
key: String::from(yaml["key"].as_str().unwrap()),
domains: yaml["domains"].as_vec().unwrap().into_iter().map(|y| create_cloudflare_domain_config_from_yaml(&y).unwrap()).collect(),
})
}
fn create_ddns_domain_config_from_yaml(yaml: &Yaml) -> Result<DDNSDomain, ConfigError> {
Result::Ok(DDNSDomain {
domain: String::from(yaml["domain"].as_str().unwrap()),
subdomains: yaml["subdomains"].as_vec().unwrap().into_iter().map(|y| String::from(y.as_str().unwrap())).collect(),
})
}
fn create_ddns_config_from_yaml(yaml: &Yaml) -> Result<DDNSConfig, ConfigError> {
Result::Ok(DDNSConfig {
domains: yaml["domains"].as_vec().unwrap().into_iter().map(|y| create_ddns_domain_config_from_yaml(&y).unwrap()).collect()
})
}
fn create_config_from_yaml(config_yaml: Vec<Yaml>) -> Result<Config, ConfigError> {
let config = &config_yaml[0];
let server_config = match create_server_config_from_yaml(&config["server"]) {
Ok(v) => v,
Err(e) => return Result::Err(e)
};
let cloudflare_config = match create_cloudflare_config_from_yaml(&config["cloudflare"]) {
Ok(v) => v,
Err(e) => return Result::Err(e)
};
let ddns_config = match create_ddns_config_from_yaml(&config["ddns"]) {
Ok(v) => v,
Err(e) => return Result::Err(e)
};
Result::Ok(Config {
server: server_config,
cloudflare: cloudflare_config,
ddns: ddns_config,
})
}
pub fn load_config(path: &str) -> Result<Config, ConfigError> {
let yaml_load = YamlLoader::load_from_str(path);
match yaml_load {
Ok(config_yaml) => create_config_from_yaml(config_yaml),
Err(_e) => Result::Err(ConfigError{})
}
}

11
src/main.rs

@ -10,10 +10,12 @@ extern crate env_logger;
extern crate futures;
extern crate num_cpus;
extern crate serde;
extern crate yaml_rust;
use clap::{App, Arg};
mod server;
mod config;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
@ -27,7 +29,7 @@ fn main() {
let args = App::new("Dynamic DNS Server")
.version(VERSION)
.author("Drew Short <warrick@sothr.com>")
.about("Recieve DDNS requests and update cloudflare subdomains")
.about("Receive DDNS requests and update associated cloudflare subdomains")
.args(&[
Arg::with_name("config")
.short("c")
@ -59,8 +61,9 @@ fn main() {
])
.get_matches();
let config: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml");
let host: &str = args.value_of("host").unwrap_or("localhost");
let config_path: &str = args.value_of("config").unwrap_or("/etc/rsddns/rsddns.yml");
let config = config::load_config(config_path).unwrap();
let host: &str = args.value_of("host").unwrap_or(&config.server.host);
let port: i32 = args
.value_of("port")
.unwrap()
@ -73,7 +76,7 @@ fn main() {
info!(
"Starting server on {}:{} with workers={} and config {}",
host, port, workers, config
host, port, workers, config_path
);
actix_web::server::new(|| server::router::create())

Loading…
Cancel
Save