add prefix_url to config options to allow polaris to run behind a reverse proxy

This commit is contained in:
João Oliveira 2017-10-01 00:47:06 +01:00
parent 69feab3bcc
commit a82af0f0b8
5 changed files with 92 additions and 59 deletions

View file

@ -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<String>,
pub reindex_every_n_seconds: Option<i32>,
pub mount_dirs: Option<Vec<MountPoint>>,
pub prefix_url: Option<String>,
pub users: Option<Vec<ConfigUser>>,
pub ydns: Option<DDNSConfig>,
}
@ -83,15 +85,17 @@ pub fn read<T>(db: &T) -> Result<Config>
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))
@ -214,6 +218,12 @@ pub fn amend<T>(db: &T, new_config: &Config) -> Result<()>
.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())?;
}
Ok(())
}
@ -244,6 +254,7 @@ 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(),
@ -259,6 +270,7 @@ fn test_amend() {
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(),
@ -298,6 +310,7 @@ 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(),
@ -320,6 +333,7 @@ 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(),
@ -357,6 +371,7 @@ 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(),
@ -379,6 +394,7 @@ 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(),

View file

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

View file

@ -0,0 +1 @@
ALTER TABLE misc_settings ADD COLUMN prefix_url TEXT NOT NULL DEFAULT "";

Binary file not shown.

View file

@ -145,6 +145,7 @@ fn run() -> 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"))?;