diff options
author | Daniel García <[email protected]> | 2019-02-02 17:45:25 +0100 |
---|---|---|
committer | Daniel García <[email protected]> | 2019-02-06 17:34:29 +0100 |
commit | ade293cf524e9cd66db8416dd3417077eb11ed07 (patch) | |
tree | fa76c3f704058acdce8429c1d1f834ef230a0802 | |
parent | 877408b80832c37725c11d5c2ce48fb08f3f7900 (diff) | |
download | vaultwarden-ade293cf524e9cd66db8416dd3417077eb11ed07.tar.gz vaultwarden-ade293cf524e9cd66db8416dd3417077eb11ed07.zip |
Save config
-rw-r--r-- | src/api/admin.rs | 2 | ||||
-rw-r--r-- | src/config.rs | 19 |
2 files changed, 14 insertions, 7 deletions
diff --git a/src/api/admin.rs b/src/api/admin.rs index a9ad767e..84b3fbc5 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -98,7 +98,7 @@ impl AdminTemplateData { Self { users, page_content: String::from("admin/page"), - config: serde_json::to_string_pretty(&CONFIG.get_config()).unwrap(), + config: CONFIG.get_config(), } } diff --git a/src/config.rs b/src/config.rs index ff65d468..945fa76a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,7 @@ lazy_static! { println!("Error loading config:\n\t{:?}\n", e); exit(12) }); + pub static ref CONFIG_PATH: String = "data/config.json".into(); } macro_rules! make_config { @@ -86,7 +87,7 @@ macro_rules! make_config { // TODO: Get config.json from CONFIG_PATH env var or -c <CONFIG> console option // Loading from file - let mut builder = match ConfigBuilder::from_file("data/config.json") { + let mut builder = match ConfigBuilder::from_file(&CONFIG_PATH) { Ok(builder) => builder, Err(_) => ConfigBuilder::default() }; @@ -200,17 +201,23 @@ fn validate_config(cfg: &ConfigItems) -> Result<(), Error> { } impl Config { - pub fn get_config(&self) -> ConfigItems { - self.inner.read().unwrap().config.clone() + pub fn get_config(&self) -> String { + let cfg = &self.inner.read().unwrap().config; + serde_json::to_string_pretty(cfg).unwrap() } - + pub fn update_config(&self, other: ConfigBuilder) -> Result<(), Error> { let config = other.build(); validate_config(&config)?; - self.inner.write().unwrap().config = config; + let config_str = serde_json::to_string_pretty(&config)?; + + self.inner.write().unwrap().config = config.clone(); - // TODO: Save to file + //Save to file + use std::{fs::File, io::Write}; + let mut file = File::create(&*CONFIG_PATH)?; + file.write_all(config_str.as_bytes())?; Ok(()) } |