From 69f1392663f3532615b826dc92e35dfb03693892 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sat, 1 Jul 2017 15:26:25 -0700 Subject: [PATCH] Added API end point to read config --- src/api.rs | 19 ++++++++++++++++- src/config.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++---- src/ddns.rs | 2 +- src/vfs.rs | 2 +- 4 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/api.rs b/src/api.rs index a6779b4..e734a76 100644 --- a/src/api.rs +++ b/src/api.rs @@ -15,6 +15,7 @@ use std::sync::Arc; use typemap; use url::percent_encoding::percent_decode; +use config; use config::MiscSettings; use db::{ConnectionSource, DB}; use db::misc_settings; @@ -26,7 +27,7 @@ use utils::*; use vfs::VFSSource; const CURRENT_MAJOR_VERSION: i32 = 2; -const CURRENT_MINOR_VERSION: i32 = 0; +const CURRENT_MINOR_VERSION: i32 = 1; #[derive(Serialize)] struct Version { @@ -120,6 +121,12 @@ fn get_endpoints(db: Arc) -> Mount { auth_api_mount.mount("/serve/", move |request: &mut Request| self::serve(request, db.deref())); } + { + let db = db.clone(); + auth_api_mount.mount("/settings/", move |request: &mut Request| { + self::get_config(request, db.deref()) + }); + } let mut auth_api_chain = Chain::new(auth_api_mount); let auth = AuthRequirement { db: db.clone() }; @@ -322,3 +329,13 @@ fn art(_: &mut Request, real_path: &Path) -> IronResult { Err(e) => Err(IronError::from(e)), } } + +fn get_config(_: &mut Request, db: &DB) -> IronResult { + let c = config::read(db)?; + let result_json = serde_json::to_string(&c); + let result_json = match result_json { + Ok(j) => j, + Err(e) => return Err(IronError::new(e, status::InternalServerError)), + }; + Ok(Response::with((status::Ok, result_json))) +} diff --git a/src/config.rs b/src/config.rs index a0c6465..f1c2aee 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,7 @@ use std::path; use toml; use db::ConnectionSource; -use db::{misc_settings, mount_points, users}; +use db::{ddns_config, misc_settings, mount_points, users}; use ddns::DDNSConfig; use errors::*; use user::*; @@ -22,16 +22,16 @@ pub struct MiscSettings { pub index_album_art_pattern: String, } -#[derive(Deserialize)] +#[derive(Serialize, Deserialize)] pub struct ConfigUser { pub name: String, pub password: String, } -#[derive(Deserialize)] +#[derive(Serialize, Deserialize)] pub struct Config { pub album_art_pattern: Option, - pub reindex_every_n_seconds: Option, + pub reindex_every_n_seconds: Option, pub mount_dirs: Option>, pub users: Option>, pub ydns: Option, @@ -59,6 +59,57 @@ pub fn parse(path: &path::Path) -> Result { Ok(config) } +pub fn read(db: &T) -> Result + where T: ConnectionSource +{ + use self::misc_settings::dsl::*; + use self::mount_points::dsl::*; + use self::ddns_config::dsl::*; + + let connection = db.get_connection(); + let connection = connection.lock().unwrap(); + let connection = connection.deref(); + + let mut config = Config { + album_art_pattern: None, + reindex_every_n_seconds: None, + mount_dirs: None, + users: None, + ydns: None, + }; + + let (art_pattern, sleep_duration) = misc_settings + .select((index_album_art_pattern, index_sleep_duration_seconds)) + .get_result(connection)?; + config.album_art_pattern = Some(art_pattern); + config.reindex_every_n_seconds = Some(sleep_duration); + + let mount_dirs = mount_points + .select((source, name)) + .get_results(connection)?; + config.mount_dirs = Some(mount_dirs); + + let usernames: Vec = users::table + .select(users::columns::name) + .get_results(connection)?; + config.users = Some(usernames + .into_iter() + .map(|s| { + ConfigUser { + name: s, + password: "".to_owned(), + } + }) + .collect::<_>()); + + let ydns = ddns_config + .select((host, username, password)) + .get_result(connection)?; + config.ydns = Some(ydns); + + Ok(config) +} + fn reset(db: &T) -> Result<()> where T: ConnectionSource { diff --git a/src/ddns.rs b/src/ddns.rs index 854eae4..b1fe6b7 100644 --- a/src/ddns.rs +++ b/src/ddns.rs @@ -10,7 +10,7 @@ use db::{ConnectionSource, DB}; use db::ddns_config; use errors; -#[derive(Debug, Deserialize, Queryable)] +#[derive(Debug, Deserialize, Queryable, Serialize)] pub struct DDNSConfig { pub host: String, pub username: String, diff --git a/src/vfs.rs b/src/vfs.rs index 5198c53..7f10fe3 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -29,7 +29,7 @@ impl VFSSource for DB { } } -#[derive(Debug, Deserialize, Insertable, Queryable)] +#[derive(Debug, Deserialize, Insertable, Queryable, Serialize)] #[table_name="mount_points"] pub struct MountPoint { pub source: String,