From 5ebdbaa225665cbc23c24e12976577d152dfb3fd Mon Sep 17 00:00:00 2001 From: _ <> Date: Tue, 27 Apr 2021 17:22:07 -0500 Subject: [PATCH] :construction: wip: machine-editable config --- crates/ptth_relay/src/config.rs | 18 ++++++++++++++++-- crates/ptth_relay/src/lib.rs | 14 +++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/ptth_relay/src/config.rs b/crates/ptth_relay/src/config.rs index 74ab74e..b59120a 100644 --- a/crates/ptth_relay/src/config.rs +++ b/crates/ptth_relay/src/config.rs @@ -19,14 +19,28 @@ use crate::{ /// These are stored in the `data` directory and shouldn't be touched by /// humans. ptth_relay will re-write them while it's running. -mod machine_editable { +pub mod machine_editable { + use std::{ + path::Path, + }; + use serde::{Deserialize, Serialize}; use super::file::Server; #[derive (Deserialize, Serialize)] pub struct Config { - pub servers: Option >, + pub servers: Vec , + } + + impl Config { + pub async fn from_file (path: &Path) -> Result + { + let config_s = tokio::fs::read_to_string (path).await?; + let new_config: Config = toml::from_str (&config_s)?; + + Ok (new_config) + } } } diff --git a/crates/ptth_relay/src/lib.rs b/crates/ptth_relay/src/lib.rs index 841c176..d83feb4 100644 --- a/crates/ptth_relay/src/lib.rs +++ b/crates/ptth_relay/src/lib.rs @@ -70,7 +70,10 @@ mod routing; mod scraper_api; mod server_endpoint; -pub use config::Config; +pub use config::{ + Config, + machine_editable, +}; pub use errors::*; pub use relay_state::Relay; @@ -588,15 +591,20 @@ async fn reload_config ( state: &Arc , config_reload_path: &Path ) -> Result <(), ConfigError> { + // Reload human-editable config let new_config = Config::from_file (config_reload_path).await?; + + // Reload machine-editable config, if possible + let me_config = machine_editable::Config::from_file (Path::new ("data/ptth_relay_me_config.toml")).await.ok (); + let mut config = state.config.write ().await; trace! ("Reloading config"); if config.servers.len () != new_config.servers.len () { - debug! ("Loaded {} server configs", config.servers.len ()); + debug! ("Loaded {} server configs", new_config.servers.len ()); } if config.iso.enable_scraper_api != new_config.iso.enable_scraper_api { - debug! ("enable_scraper_api: {}", config.iso.enable_scraper_api); + debug! ("enable_scraper_api: {}", new_config.iso.enable_scraper_api); } (*config) = new_config;