Unify DDNS config struct

This commit is contained in:
Antoine Gersant 2017-06-28 23:58:24 -07:00
parent 96e6b86f04
commit d87ec09c38
4 changed files with 26 additions and 30 deletions

View file

@ -6,6 +6,7 @@ use toml;
use errors::*; use errors::*;
use db::NewMountPoint; use db::NewMountPoint;
use ddns::DDNSConfig;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct User { pub struct User {
@ -13,13 +14,6 @@ pub struct User {
pub password: String, pub password: String,
} }
#[derive(Deserialize)]
pub struct DDNSConfig {
pub host: String,
pub username: String,
pub password: String,
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct UserConfig { pub struct UserConfig {
pub album_art_pattern: Option<String>, pub album_art_pattern: Option<String>,

View file

@ -10,6 +10,7 @@ use std::sync::{Arc, Mutex};
use config::UserConfig; use config::UserConfig;
use db::schema::*; use db::schema::*;
use ddns::{DDNSConfigSource, DDNSConfig};
use errors::*; use errors::*;
use vfs::Vfs; use vfs::Vfs;
@ -119,12 +120,6 @@ impl DB {
Ok(misc.auth_secret.to_owned()) Ok(misc.auth_secret.to_owned())
} }
pub fn get_ddns_config(&self) -> Result<DDNSConfig> {
let connection = self.connection.lock().unwrap();
let connection = connection.deref();
Ok(ddns_config::table.get_result(connection)?)
}
pub fn locate(&self, virtual_path: &Path) -> Result<PathBuf> { pub fn locate(&self, virtual_path: &Path) -> Result<PathBuf> {
let vfs = self.get_vfs()?; let vfs = self.get_vfs()?;
vfs.virtual_to_real(virtual_path) vfs.virtual_to_real(virtual_path)
@ -269,6 +264,14 @@ impl DB {
} }
} }
impl DDNSConfigSource for DB {
fn get_ddns_config(&self) -> Result<DDNSConfig> {
let connection = self.connection.lock().unwrap();
let connection = connection.deref();
Ok(ddns_config::table.select((ddns_config::columns::host, ddns_config::columns::username, ddns_config::columns::password)).get_result(connection)?)
}
}
fn _get_test_db(name: &str) -> DB { fn _get_test_db(name: &str) -> DB {
let config_path = Path::new("test/config.toml"); let config_path = Path::new("test/config.toml");
let config = UserConfig::parse(&config_path).unwrap(); let config = UserConfig::parse(&config_path).unwrap();

View file

@ -115,12 +115,3 @@ pub struct MiscSettings {
} }
// DDNS Settings
#[derive(Debug, Deserialize, Queryable)]
pub struct DDNSConfig {
id : i32,
pub host: String,
pub username: String,
pub password: String,
}

View file

@ -4,9 +4,18 @@ use std::io;
use std::thread; use std::thread;
use std::time; use std::time;
use db::DB;
use errors; use errors;
#[derive(Debug, Deserialize, Queryable)]
pub struct DDNSConfig {
pub host: String,
pub username: String,
pub password: String,
}
pub trait DDNSConfigSource {
fn get_ddns_config(&self) -> errors::Result<DDNSConfig>;
}
#[derive(Debug)] #[derive(Debug)]
enum DDNSError { enum DDNSError {
@ -37,8 +46,8 @@ impl From<reqwest::Error> for DDNSError {
const DDNS_UPDATE_URL: &'static str = "https://ydns.io/api/v1/update/"; const DDNS_UPDATE_URL: &'static str = "https://ydns.io/api/v1/update/";
fn update_my_ip(db: &DB) -> Result<(), DDNSError> { fn update_my_ip<T>(config_source: &T) -> Result<(), DDNSError> where T: DDNSConfigSource {
let config = db.get_ddns_config()?; let config = config_source.get_ddns_config()?;
if config.host.len() == 0 || config.username.len() == 0 { if config.host.len() == 0 || config.username.len() == 0 {
println!("Skipping DDNS update because credentials are missing"); println!("Skipping DDNS update because credentials are missing");
return Ok(()); return Ok(());
@ -60,12 +69,11 @@ fn update_my_ip(db: &DB) -> Result<(), DDNSError> {
Ok(()) Ok(())
} }
pub fn run(db: &DB) { pub fn run<T>(config_source: &T) where T: DDNSConfigSource {
loop { loop {
match update_my_ip(db) { if let Err(e) = update_my_ip(config_source) {
Err(e) => println!("Dynamic DNS Error: {:?}", e), println!("Dynamic DNS update error: {:?}", e);
Ok(_) => (), }
};
thread::sleep(time::Duration::from_secs(60 * 30)); thread::sleep(time::Duration::from_secs(60 * 30));
} }
} }