Browse Source

Ran code formatting and cleaned imports.

develop
Drew Short 6 years ago
parent
commit
9cad1dfc91
  1. 16
      src/config/default.rs
  2. 6
      src/config/error.rs
  3. 7
      src/config/load.rs
  4. 17
      src/config/model.rs
  5. 15
      src/main.rs
  6. 21
      src/server/api.rs
  7. 2
      src/server/router.rs

16
src/config/default.rs

@ -5,7 +5,7 @@ fn get_default_config() -> Config {
server: ServerConfig {
host: Option::Some(String::from("localhost")),
port: Option::Some(8080),
workers: Option::Some(4)
workers: Option::Some(4),
},
cloudflare: CloudflareConfig {
email: String::from("something@something.com"),
@ -13,9 +13,9 @@ fn get_default_config() -> Config {
domains: vec![
CloudflareDomainConfig {
domain: String::from("IAmNotADomain.com"),
zone_id: String::from("IAmNotAZoneID")
zone_id: String::from("IAmNotAZoneID"),
}
]
],
},
ddns: DDNSConfig {
domains: vec![
@ -23,7 +23,7 @@ fn get_default_config() -> Config {
domain: String::from("IAmNotADomain.com"),
subdomains: vec![
String::from("ddns")
]
],
}
]
},
@ -39,12 +39,12 @@ fn get_default_config() -> Config {
subdomain: String::from("ddns"),
targets: vec![
String::from("home")
]
],
}
]
],
}
]
],
}
]
],
}
}

6
src/config/error.rs

@ -4,14 +4,14 @@ use std::fmt;
#[derive(Debug)]
pub struct ConfigError {
description: String,
original_error: Option<Box<Error>>
original_error: Option<Box<Error>>,
}
impl ConfigError {
pub fn new(description: &str, original_error: Option<Box<Error>>) -> ConfigError {
ConfigError {
description: String::from(description),
original_error
original_error,
}
}
}
@ -23,7 +23,7 @@ impl fmt::Display for ConfigError {
match &self.original_error {
Some(original_error) => {
write!(f, "{}: \"{}\"", self.description, original_error)
},
}
None => write!(f, "{}", self.description)
}
}

7
src/config/load.rs

@ -1,7 +1,8 @@
use serde_yaml;
use std::fs::File;
use std::io::prelude::*;
use serde_yaml;
use config::error::ConfigError;
use config::model::Config;
@ -23,10 +24,10 @@ pub fn read(path: &str) -> Result<Config, ConfigError> {
} 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))
}
}

17
src/config/model.rs

@ -1,16 +1,15 @@
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ServerConfig {
pub host: Option<String>,
pub port: Option<i32>,
pub workers: Option<usize>
pub workers: Option<usize>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct CloudflareDomainConfig {
pub domain: String,
pub zone_id: String
pub zone_id: String,
}
@ -18,14 +17,14 @@ pub struct CloudflareDomainConfig {
pub struct CloudflareConfig {
pub domains: Vec<CloudflareDomainConfig>,
pub key: String,
pub email: String
pub email: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct DDNSDomain {
pub domain: String,
pub subdomains: Vec<String>
pub subdomains: Vec<String>,
}
@ -38,14 +37,14 @@ pub struct DDNSConfig {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserSubdomainConfig {
pub subdomain: String,
pub targets: Vec<String>
pub targets: Vec<String>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserDomainConfig {
pub domain: String,
pub subdomains: Vec<UserSubdomainConfig>
pub subdomains: Vec<UserSubdomainConfig>,
}
@ -53,7 +52,7 @@ pub struct UserDomainConfig {
pub struct UserConfig {
pub username: String,
pub token: String,
pub domains: Vec<UserDomainConfig>
pub domains: Vec<UserDomainConfig>,
}
@ -62,5 +61,5 @@ pub struct Config {
pub server: ServerConfig,
pub cloudflare: CloudflareConfig,
pub ddns: DDNSConfig,
pub users: Vec<UserConfig>
pub users: Vec<UserConfig>,
}

15
src/main.rs

@ -1,17 +1,16 @@
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;
extern crate actix_web;
extern crate bytes;
extern crate clap;
extern crate env_logger;
extern crate futures;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate num_cpus;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_yaml;
use clap::{App, Arg};
@ -77,7 +76,7 @@ fn main() {
Ok(c) => {
info!("Loaded configuration from \"{}\"", config_path);
Option::Some(c)
},
}
Err(e) => {
error!("{}", e);
Option::None

21
src/server/api.rs

@ -1,20 +1,21 @@
use std::io::Read;
use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Scope};
use actix_web::http::Method;
use actix_web::http::StatusCode;
use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Scope};
use bytes::{Buf, Bytes, IntoBuf};
use futures::future::Future;
use std::io::Read;
pub fn route(scope: Scope<()>) -> Scope<()> {
scope.resource("address", |r| r.method(Method::GET).f(get_address))
.nested("{root}/{zone}", |zone_scope| {
zone_scope
.resource("", |r| r.method(Method::GET).f(get_address))
.resource("update", |r| {
r.method(Method::GET).f(update_address_automatically);
r.method(Method::POST).f(update_address_manually)
})
})
.nested("{root}/{zone}", |zone_scope| {
zone_scope
.resource("", |r| r.method(Method::GET).f(get_address))
.resource("update", |r| {
r.method(Method::GET).f(update_address_automatically);
r.method(Method::POST).f(update_address_manually)
})
})
}
fn update_address(address: String) -> String {

2
src/server/router.rs

@ -1,7 +1,7 @@
extern crate actix_web;
use actix_web::{App, http};
use actix_web::middleware::Logger;
use actix_web::{http, App};
use server;

Loading…
Cancel
Save