🚧 wip: machine-editable config

main
_ 2021-04-27 17:22:07 -05:00
parent c361ecd806
commit 5ebdbaa225
2 changed files with 27 additions and 5 deletions

View File

@ -19,14 +19,28 @@ use crate::{
/// These are stored in the `data` directory and shouldn't be touched by /// These are stored in the `data` directory and shouldn't be touched by
/// humans. ptth_relay will re-write them while it's running. /// 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 serde::{Deserialize, Serialize};
use super::file::Server; use super::file::Server;
#[derive (Deserialize, Serialize)] #[derive (Deserialize, Serialize)]
pub struct Config { pub struct Config {
pub servers: Option <Vec <Server>>, pub servers: Vec <Server>,
}
impl Config {
pub async fn from_file (path: &Path) -> Result <Self, crate::ConfigError>
{
let config_s = tokio::fs::read_to_string (path).await?;
let new_config: Config = toml::from_str (&config_s)?;
Ok (new_config)
}
} }
} }

View File

@ -70,7 +70,10 @@ mod routing;
mod scraper_api; mod scraper_api;
mod server_endpoint; mod server_endpoint;
pub use config::Config; pub use config::{
Config,
machine_editable,
};
pub use errors::*; pub use errors::*;
pub use relay_state::Relay; pub use relay_state::Relay;
@ -588,15 +591,20 @@ async fn reload_config (
state: &Arc <Relay>, state: &Arc <Relay>,
config_reload_path: &Path config_reload_path: &Path
) -> Result <(), ConfigError> { ) -> Result <(), ConfigError> {
// Reload human-editable config
let new_config = Config::from_file (config_reload_path).await?; 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; let mut config = state.config.write ().await;
trace! ("Reloading config"); trace! ("Reloading config");
if config.servers.len () != new_config.servers.len () { 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 { 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; (*config) = new_config;