From a82af0f0b8b438cf3cbf1e6c78860432101725a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Sun, 1 Oct 2017 00:47:06 +0100 Subject: [PATCH] add prefix_url to config options to allow polaris to run behind a reverse proxy --- src/config.rs | 126 ++++++++++-------- .../20170929203228_add_prefix_url/down.sql | 11 ++ .../20170929203228_add_prefix_url/up.sql | 1 + src/db/schema.sqlite | Bin 69632 -> 69632 bytes src/main.rs | 13 +- 5 files changed, 92 insertions(+), 59 deletions(-) create mode 100644 src/db/migrations/20170929203228_add_prefix_url/down.sql create mode 100644 src/db/migrations/20170929203228_add_prefix_url/up.sql diff --git a/src/config.rs b/src/config.rs index 8d3f815..9b8204b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,6 +22,7 @@ pub struct MiscSettings { pub auth_secret: String, pub index_sleep_duration_seconds: i32, pub index_album_art_pattern: String, + pub prefix_url: String, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -36,6 +37,7 @@ pub struct Config { pub album_art_pattern: Option, pub reindex_every_n_seconds: Option, pub mount_dirs: Option>, + pub prefix_url: Option, pub users: Option>, pub ydns: Option, } @@ -83,15 +85,17 @@ pub fn read(db: &T) -> Result album_art_pattern: None, reindex_every_n_seconds: None, mount_dirs: None, + prefix_url: None, users: None, ydns: None, }; - let (art_pattern, sleep_duration) = misc_settings - .select((index_album_art_pattern, index_sleep_duration_seconds)) + let (art_pattern, sleep_duration, url) = misc_settings + .select((index_album_art_pattern, index_sleep_duration_seconds, prefix_url)) .get_result(connection.deref())?; config.album_art_pattern = Some(art_pattern); config.reindex_every_n_seconds = Some(sleep_duration); + config.prefix_url = if url != "" { Some(url) } else { None }; let mount_dirs = mount_points .select((source, name)) @@ -102,15 +106,15 @@ pub fn read(db: &T) -> Result .select((users::columns::name, users::columns::admin)) .get_results(connection.deref())?; config.users = Some(found_users - .into_iter() - .map(|(n, a)| { - ConfigUser { - name: n, - password: "".to_owned(), - admin: a != 0, - } - }) - .collect::<_>()); + .into_iter() + .map(|(n, a)| { + ConfigUser { + name: n, + password: "".to_owned(), + admin: a != 0, + } + }) + .collect::<_>()); let ydns = ddns_config .select((host, username, password)) @@ -166,9 +170,9 @@ pub fn amend(db: &T, new_config: &Config) -> Result<()> let delete_usernames: Vec = old_usernames .into_iter() .filter(|old_name| match config_users.iter().find(|u| &u.name == old_name) { - None => true, - Some(new_user) => !new_user.password.is_empty(), - }) + None => true, + Some(new_user) => !new_user.password.is_empty(), + }) .collect::<_>(); diesel::delete(users::table.filter(users::name.eq_any(&delete_usernames))) .execute(connection.deref())?; @@ -209,8 +213,14 @@ pub fn amend(db: &T, new_config: &Config) -> Result<()> use self::ddns_config::dsl::*; diesel::update(ddns_config) .set((host.eq(ydns.host.clone()), - username.eq(ydns.username.clone()), - password.eq(ydns.password.clone()))) + username.eq(ydns.username.clone()), + password.eq(ydns.password.clone()))) + .execute(connection.deref())?; + } + + if let Some(ref prefix_url) = new_config.prefix_url { + diesel::update(misc_settings::table) + .set(misc_settings::prefix_url.eq(prefix_url)) .execute(connection.deref())?; } @@ -244,35 +254,37 @@ fn test_amend() { let initial_config = Config { album_art_pattern: Some("file\\.png".into()), reindex_every_n_seconds: Some(123), + prefix_url: None, mount_dirs: Some(vec![MountPoint { - source: "C:\\Music".into(), - name: "root".into(), - }]), + source: "C:\\Music".into(), + name: "root".into(), + }]), users: Some(vec![ConfigUser { - name: "Teddy🐻".into(), - password: "Tasty🍖".into(), - admin: false, - }]), + name: "Teddy🐻".into(), + password: "Tasty🍖".into(), + admin: false, + }]), ydns: None, }; let new_config = Config { album_art_pattern: Some("🖼️\\.jpg".into()), reindex_every_n_seconds: None, + prefix_url: Some("polaris".into()), mount_dirs: Some(vec![MountPoint { - source: "/home/music".into(), - name: "🎵📁".into(), - }]), + source: "/home/music".into(), + name: "🎵📁".into(), + }]), users: Some(vec![ConfigUser { - name: "Kermit🐸".into(), - password: "🐞🐞".into(), - admin: false, - }]), + name: "Kermit🐸".into(), + password: "🐞🐞".into(), + admin: false, + }]), ydns: Some(DDNSConfig { - host: "🐸🐸🐸.ydns.eu".into(), - username: "kfr🐸g".into(), - password: "tasty🐞".into(), - }), + host: "🐸🐸🐸.ydns.eu".into(), + username: "kfr🐸g".into(), + password: "tasty🐞".into(), + }), }; let mut expected_config = new_config.clone(); @@ -298,12 +310,13 @@ fn test_amend_preserve_password_hashes() { let initial_config = Config { album_art_pattern: None, reindex_every_n_seconds: None, + prefix_url: None, mount_dirs: None, users: Some(vec![ConfigUser { - name: "Teddy🐻".into(), - password: "Tasty🍖".into(), - admin: false, - }]), + name: "Teddy🐻".into(), + password: "Tasty🍖".into(), + admin: false, + }]), ydns: None, }; amend(&db, &initial_config).unwrap(); @@ -320,17 +333,18 @@ fn test_amend_preserve_password_hashes() { let new_config = Config { album_art_pattern: None, reindex_every_n_seconds: None, + prefix_url: None, mount_dirs: None, users: Some(vec![ConfigUser { - name: "Kermit🐸".into(), - password: "tasty🐞".into(), - admin: false, - }, - ConfigUser { - name: "Teddy🐻".into(), - password: "".into(), - admin: false, - }]), + name: "Kermit🐸".into(), + password: "tasty🐞".into(), + admin: false, + }, + ConfigUser { + name: "Teddy🐻".into(), + password: "".into(), + admin: false, + }]), ydns: None, }; amend(&db, &new_config).unwrap(); @@ -357,12 +371,13 @@ fn test_toggle_admin() { let initial_config = Config { album_art_pattern: None, reindex_every_n_seconds: None, + prefix_url: None, mount_dirs: None, users: Some(vec![ConfigUser { - name: "Teddy🐻".into(), - password: "Tasty🍖".into(), - admin: true, - }]), + name: "Teddy🐻".into(), + password: "Tasty🍖".into(), + admin: true, + }]), ydns: None, }; amend(&db, &initial_config).unwrap(); @@ -379,12 +394,13 @@ fn test_toggle_admin() { let new_config = Config { album_art_pattern: None, reindex_every_n_seconds: None, + prefix_url: None, mount_dirs: None, users: Some(vec![ConfigUser { - name: "Teddy🐻".into(), - password: "".into(), - admin: false, - }]), + name: "Teddy🐻".into(), + password: "".into(), + admin: false, + }]), ydns: None, }; amend(&db, &new_config).unwrap(); diff --git a/src/db/migrations/20170929203228_add_prefix_url/down.sql b/src/db/migrations/20170929203228_add_prefix_url/down.sql new file mode 100644 index 0000000..33b8650 --- /dev/null +++ b/src/db/migrations/20170929203228_add_prefix_url/down.sql @@ -0,0 +1,11 @@ +CREATE TEMPORARY TABLE misc_settings_backup(id, auth_secret, index_sleep_duration_seconds, index_album_art_pattern); +INSERT INTO misc_settings_backup SELECT id, auth_secret, index_sleep_duration_seconds, index_album_art_pattern FROM misc_settings; +DROP TABLE misc_settings; +CREATE TABLE misc_settings ( + id INTEGER PRIMARY KEY NOT NULL CHECK(id = 0), + auth_secret TEXT NOT NULL, + index_sleep_duration_seconds INTEGER NOT NULL, + index_album_art_pattern TEXT NOT NULL +); +INSERT INTO misc_settings SELECT * FROM misc_settings_backup; +DROP TABLE misc_settings_backup; diff --git a/src/db/migrations/20170929203228_add_prefix_url/up.sql b/src/db/migrations/20170929203228_add_prefix_url/up.sql new file mode 100644 index 0000000..cc16d29 --- /dev/null +++ b/src/db/migrations/20170929203228_add_prefix_url/up.sql @@ -0,0 +1 @@ +ALTER TABLE misc_settings ADD COLUMN prefix_url TEXT NOT NULL DEFAULT ""; diff --git a/src/db/schema.sqlite b/src/db/schema.sqlite index 7273458004d2865e5ca994d9835cbb271e1f82d9..02c366203ff9b346a9046113f039e86dd999b921 100644 GIT binary patch delta 254 zcmZozz|ydQWrDPz00RSqAP~a<_e33Ic>xAJRtH|rLI z`FvRNH`aY*=4uk+WEWRgXKYTMoXz=EO-G@iC^ap!BEGaJM8b1LV2HW3cKdkp+{_;>J6;VNMp}B#jk)@G=v5}Dlh^K2{scUSYU}R)vVs2$(u$V=jQG|oPoPqxX|9$>* h{CoM=@XzM&;xFH Result<()> { let config = config::parse_toml_file(&path)?; config::overwrite(db.deref(), &config)?; } + let config = config::read(db.deref())?; // Init index let (index_sender, index_receiver) = channel(); @@ -161,13 +162,17 @@ fn run() -> Result<()> { std::thread::spawn(move || { index::self_trigger(db_ref.deref(), sender_ref); }); // Mount API - println!("Mounting API"); + let prefix_url = config.prefix_url.unwrap_or("".to_string()); + let api_url = format!("{}/api", &prefix_url); + println!("Mounting API on {}", api_url); let mut mount = Mount::new(); let handler = api::get_handler(db.clone(), index_sender)?; - mount.mount("/api/", handler); + mount.mount(&api_url, handler); // Mount static files - println!("Mounting static files"); + let static_url = format!("/{}", &prefix_url); + + println!("Mounting static files on {}", static_url); let web_dir_name = matches.opt_str("w"); let mut default_web_dir = utils::get_data_root()?; default_web_dir.push("web"); @@ -175,7 +180,7 @@ fn run() -> Result<()> { .map(|n| Path::new(n.as_str()).to_path_buf()) .unwrap_or(default_web_dir); - mount.mount("/", Static::new(web_dir_path)); + mount.mount(&static_url, Static::new(web_dir_path)); println!("Starting up server"); let port: u16 = matches.opt_str("p").unwrap_or("5050".to_owned()).parse().or(Err("invalid port number"))?;