diff --git a/src/config.rs b/src/config.rs index bb14daf..82441d2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,6 +6,7 @@ use toml; use errors::*; use db::NewMountPoint; +use ddns::DDNSConfig; #[derive(Deserialize)] pub struct User { @@ -13,13 +14,6 @@ pub struct User { pub password: String, } -#[derive(Deserialize)] -pub struct DDNSConfig { - pub host: String, - pub username: String, - pub password: String, -} - #[derive(Deserialize)] pub struct UserConfig { pub album_art_pattern: Option, diff --git a/src/db/mod.rs b/src/db/mod.rs index 183884f..4c8047b 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -10,6 +10,7 @@ use std::sync::{Arc, Mutex}; use config::UserConfig; use db::schema::*; +use ddns::{DDNSConfigSource, DDNSConfig}; use errors::*; use vfs::Vfs; @@ -119,12 +120,6 @@ impl DB { Ok(misc.auth_secret.to_owned()) } - pub fn get_ddns_config(&self) -> Result { - 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 { let vfs = self.get_vfs()?; vfs.virtual_to_real(virtual_path) @@ -269,6 +264,14 @@ impl DB { } } +impl DDNSConfigSource for DB { + fn get_ddns_config(&self) -> Result { + 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 { let config_path = Path::new("test/config.toml"); let config = UserConfig::parse(&config_path).unwrap(); diff --git a/src/db/models.rs b/src/db/models.rs index f3328ec..0b689bb 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -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, -} - diff --git a/src/ddns.rs b/src/ddns.rs index 2f5a829..61922d1 100644 --- a/src/ddns.rs +++ b/src/ddns.rs @@ -4,9 +4,18 @@ use std::io; use std::thread; use std::time; -use db::DB; 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; +} #[derive(Debug)] enum DDNSError { @@ -37,8 +46,8 @@ impl From for DDNSError { const DDNS_UPDATE_URL: &'static str = "https://ydns.io/api/v1/update/"; -fn update_my_ip(db: &DB) -> Result<(), DDNSError> { - let config = db.get_ddns_config()?; +fn update_my_ip(config_source: &T) -> Result<(), DDNSError> where T: DDNSConfigSource { + let config = config_source.get_ddns_config()?; if config.host.len() == 0 || config.username.len() == 0 { println!("Skipping DDNS update because credentials are missing"); return Ok(()); @@ -60,12 +69,11 @@ fn update_my_ip(db: &DB) -> Result<(), DDNSError> { Ok(()) } -pub fn run(db: &DB) { +pub fn run(config_source: &T) where T: DDNSConfigSource { loop { - match update_my_ip(db) { - Err(e) => println!("Dynamic DNS Error: {:?}", e), - Ok(_) => (), - }; + if let Err(e) = update_my_ip(config_source) { + println!("Dynamic DNS update error: {:?}", e); + } thread::sleep(time::Duration::from_secs(60 * 30)); } }