Added API end point to read config

This commit is contained in:
Antoine Gersant 2017-07-01 15:26:25 -07:00
parent ae4e5ab81b
commit 69f1392663
4 changed files with 75 additions and 7 deletions

View file

@ -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<DB>) -> 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<Response> {
Err(e) => Err(IronError::from(e)),
}
}
fn get_config(_: &mut Request, db: &DB) -> IronResult<Response> {
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)))
}

View file

@ -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<String>,
pub reindex_every_n_seconds: Option<u64>,
pub reindex_every_n_seconds: Option<i32>,
pub mount_dirs: Option<Vec<MountPoint>>,
pub users: Option<Vec<ConfigUser>>,
pub ydns: Option<DDNSConfig>,
@ -59,6 +59,57 @@ pub fn parse(path: &path::Path) -> Result<Config> {
Ok(config)
}
pub fn read<T>(db: &T) -> Result<Config>
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<String> = 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<T>(db: &T) -> Result<()>
where T: ConnectionSource
{

View file

@ -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,

View file

@ -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,