From 9e5bd112e7f4d4f3c0e3d2128c8e4a6d1ac57f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sat, 12 Mar 2022 00:02:25 +0300 Subject: [PATCH] feat(config): support setting the refresh rate for hot-reloading --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + config.toml | 3 +++ src/config.rs | 11 +++++++++++ src/main.rs | 19 ++++++++++++------- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e46486..61cb2f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -935,6 +935,16 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "humantime-serde" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a3db5ea5923d99402c94e9feb261dc5ee9b4efa158b0315f788cf549cc200c" +dependencies = [ + "humantime", + "serde", +] + [[package]] name = "idna" version = "0.2.3" @@ -1637,6 +1647,7 @@ dependencies = [ "glob", "hotwatch", "humantime", + "humantime-serde", "infer", "lazy-regex", "log", diff --git a/Cargo.toml b/Cargo.toml index 08dcac8..1e42c8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ regex = "1.5.4" serde_regex = "1.1.0" lazy-regex = "2.2.1" humantime = "2.1.0" +humantime-serde = "1.1.1" glob = "0.3.0" ring = "0.16.20" hotwatch = "0.4.5" diff --git a/config.toml b/config.toml index 5f83dca..76be072 100644 --- a/config.toml +++ b/config.toml @@ -1,3 +1,6 @@ +[config] +refresh_rate="1s" + [server] address="127.0.0.1:8000" #workers=4 diff --git a/src/config.rs b/src/config.rs index c71872d..30e7c75 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,16 +3,27 @@ use crate::random::RandomURLConfig; use byte_unit::Byte; use config::{self, ConfigError}; use std::path::{Path, PathBuf}; +use std::time::Duration; /// Configuration values. #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct Config { + /// Configuration settings. + pub config: Option, /// Server configuration. pub server: ServerConfig, /// Paste configuration. pub paste: PasteConfig, } +/// General settings for configuration. +#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] +pub struct Settings { + /// Refresh rate of the configuration file. + #[serde(with = "humantime_serde")] + pub refresh_rate: Duration, +} + /// Server configuration. #[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct ServerConfig { diff --git a/src/main.rs b/src/main.rs index d14979f..8792c8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,11 +21,8 @@ async fn main() -> IoResult<()> { dotenv::dotenv().ok(); let config_path = PathBuf::from(env::var("CONFIG").unwrap_or_else(|_| String::from("config.toml"))); - let config = Arc::new(RwLock::new( - Config::parse(&config_path).expect("failed to parse config"), - )); - let cloned_config = Arc::clone(&config); - let server_config = config.read().expect("cannot acquire config").server.clone(); + let config = Config::parse(&config_path).expect("failed to parse config"); + let server_config = config.server.clone(); // Create necessary directories. fs::create_dir_all(&server_config.upload_path)?; @@ -34,10 +31,18 @@ async fn main() -> IoResult<()> { } // Set up a watcher for the configuration file changes. - let mut hotwatch = Hotwatch::new_with_custom_delay(Duration::from_secs(1)) - .expect("failed to initialize configuration file watcher"); + let mut hotwatch = Hotwatch::new_with_custom_delay( + config + .config + .as_ref() + .map(|v| v.refresh_rate) + .unwrap_or_else(|| Duration::from_secs(1)), + ) + .expect("failed to initialize configuration file watcher"); // Hot-reload the configuration file. + let config = Arc::new(RwLock::new(config)); + let cloned_config = Arc::clone(&config); let config_watcher = move |event: Event| { if let Event::Write(path) = event { match Config::parse(&path) {