Unify DDNS config struct
This commit is contained in:
parent
96e6b86f04
commit
d87ec09c38
4 changed files with 26 additions and 30 deletions
|
@ -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<String>,
|
||||
|
|
|
@ -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<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> {
|
||||
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<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 {
|
||||
let config_path = Path::new("test/config.toml");
|
||||
let config = UserConfig::parse(&config_path).unwrap();
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
24
src/ddns.rs
24
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<DDNSConfig>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum DDNSError {
|
||||
|
@ -37,8 +46,8 @@ impl From<reqwest::Error> 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<T>(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<T>(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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue