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
domains:
- domain: IAmNotADomain.com
subdomains:
- subdomain: ddns
targets:
root: ddns.IAmNotADomain.com
zones:
- home

12
src/config/default.rs

@ -31,13 +31,11 @@ fn get_default_config() -> Config {
UserConfig {
username: String::from("userOne"),
token: String::from("6d37d7a9-6b6b-4db2-99f2-c261e4f4b922"),
domains: vec![
UserDomainConfig {
roots: vec![
UserRootConfig {
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")
],
}
@ -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 load;
pub mod model;
pub mod validate;

21
src/config/model.rs

@ -5,14 +5,12 @@ pub struct ServerConfig {
pub workers: Option<usize>,
}
#[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<CloudflareDomainConfig>,
@ -20,42 +18,31 @@ pub struct CloudflareConfig {
pub email: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct DDNSDomain {
pub domain: String,
pub subdomains: Vec<String>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct DDNSConfig {
pub domains: Vec<DDNSDomain>
}
#[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 subdomains: Vec<UserSubdomainConfig>,
pub root: String,
pub zones: Vec<String>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserConfig {
pub username: String,
pub token: String,
pub domains: Vec<UserDomainConfig>,
pub roots: Vec<UserRootConfig>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Config {
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;
use std::process;
use std::sync::Arc;
mod args;
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 port = args::parse::get_port(&args, &config, DEFAULT_PORT);
let workers = args::parse::get_workers(&args, &config, *DEFAULT_WORKERS);
let bind = format!("{}:{}", host, port);
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)
.bind(format!("{}:{}", host, port));
.bind(bind);
match actix_server {
Ok(server) => server.run(),

0
src/server/middleware.rs

8
src/server/router.rs

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