Browse Source

Update config to mirror the api concept of root and zone

develop
Drew Short 6 years ago
parent
commit
7fea659c6e
  1. 5
      rsddns-example.yml
  2. 12
      src/config/default.rs
  3. 1
      src/config/mod.rs
  4. 21
      src/config/model.rs
  5. 6
      src/config/validate.rs
  6. 19
      src/main.rs
  7. 0
      src/server/middleware.rs
  8. 8
      src/server/router.rs

5
rsddns-example.yml

@ -19,7 +19,6 @@ users:
token: 6d37d7a9-6b6b-4db2-99f2-c261e4f4b922 token: 6d37d7a9-6b6b-4db2-99f2-c261e4f4b922
domains: domains:
- domain: IAmNotADomain.com - domain: IAmNotADomain.com
subdomains:
- subdomain: ddns
targets:
root: ddns.IAmNotADomain.com
zones:
- home - home

12
src/config/default.rs

@ -31,13 +31,11 @@ fn get_default_config() -> Config {
UserConfig { UserConfig {
username: String::from("userOne"), username: String::from("userOne"),
token: String::from("6d37d7a9-6b6b-4db2-99f2-c261e4f4b922"), token: String::from("6d37d7a9-6b6b-4db2-99f2-c261e4f4b922"),
domains: vec![
UserDomainConfig {
roots: vec![
UserRootConfig {
domain: String::from("IAmNotADomain.com"), domain: String::from("IAmNotADomain.com"),
subdomains: vec![
UserSubdomainConfig {
subdomain: String::from("ddns"),
targets: vec![
root: String::from("ddns.IAmNotADomain.com"),
zones: vec![
String::from("home") String::from("home")
], ],
} }
@ -45,6 +43,4 @@ fn get_default_config() -> Config {
} }
], ],
} }
],
}
} }

1
src/config/mod.rs

@ -2,3 +2,4 @@ pub mod default;
pub mod error; pub mod error;
pub mod load; pub mod load;
pub mod model; pub mod model;
pub mod validate;

21
src/config/model.rs

@ -5,14 +5,12 @@ pub struct ServerConfig {
pub workers: Option<usize>, pub workers: Option<usize>,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct CloudflareDomainConfig { pub struct CloudflareDomainConfig {
pub domain: String, pub domain: String,
pub zone_id: String, pub zone_id: String,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct CloudflareConfig { pub struct CloudflareConfig {
pub domains: Vec<CloudflareDomainConfig>, pub domains: Vec<CloudflareDomainConfig>,
@ -20,42 +18,31 @@ pub struct CloudflareConfig {
pub email: String, pub email: String,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct DDNSDomain { pub struct DDNSDomain {
pub domain: String, pub domain: String,
pub subdomains: Vec<String>, pub subdomains: Vec<String>,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct DDNSConfig { pub struct DDNSConfig {
pub domains: Vec<DDNSDomain> pub domains: Vec<DDNSDomain>
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserSubdomainConfig {
pub subdomain: String,
pub targets: Vec<String>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserDomainConfig {
pub struct UserRootConfig {
pub domain: String, pub domain: String,
pub subdomains: Vec<UserSubdomainConfig>,
pub root: String,
pub zones: Vec<String>,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserConfig { pub struct UserConfig {
pub username: String, pub username: String,
pub token: String, pub token: String,
pub domains: Vec<UserDomainConfig>,
pub roots: Vec<UserRootConfig>,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Config { pub struct Config {
pub server: ServerConfig, pub server: ServerConfig,

6
src/config/validate.rs

@ -0,0 +1,6 @@
use crate::config::model::Config;
use crate::config::error::ConfigError;
pub fn validate(config: &Config) -> Result<&Config, ConfigError> {
Ok(config)
}

19
src/main.rs

@ -14,6 +14,7 @@ extern crate serde_derive;
extern crate serde_yaml; extern crate serde_yaml;
use std::process; use std::process;
use std::sync::Arc;
mod args; mod args;
mod config; mod config;
@ -51,18 +52,28 @@ fn main() {
} }
}; };
match config::validate::validate(&config) {
Ok(_) => info!("Configuration Is Valid"),
Err(e) => {
error!("{}", e);
process::exit(-1)
}
}
let host = args::parse::get_host(&args, &config, DEFAULT_HOST); let host = args::parse::get_host(&args, &config, DEFAULT_HOST);
let port = args::parse::get_port(&args, &config, DEFAULT_PORT); let port = args::parse::get_port(&args, &config, DEFAULT_PORT);
let workers = args::parse::get_workers(&args, &config, *DEFAULT_WORKERS); let workers = args::parse::get_workers(&args, &config, *DEFAULT_WORKERS);
let bind = format!("{}:{}", host, port);
info!( info!(
"Starting server on {}:{} with workers={} and config \"{}\"",
host, port, workers, config_path
"Starting server on {} with workers={} and config \"{}\"",
bind, workers, config_path
); );
let actix_server = actix_web::server::new(server::router::create)
let shared_config = Arc::new(config);
let actix_server = actix_web::server::new(server::router::factory(shared_config))
.workers(workers) .workers(workers)
.bind(format!("{}:{}", host, port));
.bind(bind);
match actix_server { match actix_server {
Ok(server) => server.run(), Ok(server) => server.run(),

0
src/server/middleware.rs

8
src/server/router.rs

@ -1,16 +1,22 @@
extern crate actix_web; extern crate actix_web;
use std::sync::Arc;
use actix_web::{App, http}; use actix_web::{App, http};
use actix_web::middleware::Logger; use actix_web::middleware::Logger;
use crate::config::model::Config;
use crate::server; use crate::server;
pub fn create() -> App {
pub fn factory(config: Arc<Config>) -> impl Fn() -> App + Send + Clone {
|| {
actix_web::App::new() actix_web::App::new()
.middleware(Logger::default()) .middleware(Logger::default())
.scope("api/", server::api::route) .scope("api/", server::api::route)
// .middleware()
.resource("/health", |r| { .resource("/health", |r| {
r.method(http::Method::GET).with(server::healthcheck) r.method(http::Method::GET).with(server::healthcheck)
}) })
.resource("/", |r| r.f(server::index)) .resource("/", |r| r.f(server::index))
} }
}
Loading…
Cancel
Save