mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2024-11-21 20:09:48 -05:00
feat(config): support setting the refresh rate for hot-reloading
This commit is contained in:
parent
0f893ba058
commit
9e5bd112e7
5 changed files with 38 additions and 7 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
@ -935,6 +935,16 @@ version = "2.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
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]]
|
[[package]]
|
||||||
name = "idna"
|
name = "idna"
|
||||||
version = "0.2.3"
|
version = "0.2.3"
|
||||||
|
@ -1637,6 +1647,7 @@ dependencies = [
|
||||||
"glob",
|
"glob",
|
||||||
"hotwatch",
|
"hotwatch",
|
||||||
"humantime",
|
"humantime",
|
||||||
|
"humantime-serde",
|
||||||
"infer",
|
"infer",
|
||||||
"lazy-regex",
|
"lazy-regex",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -29,6 +29,7 @@ regex = "1.5.4"
|
||||||
serde_regex = "1.1.0"
|
serde_regex = "1.1.0"
|
||||||
lazy-regex = "2.2.1"
|
lazy-regex = "2.2.1"
|
||||||
humantime = "2.1.0"
|
humantime = "2.1.0"
|
||||||
|
humantime-serde = "1.1.1"
|
||||||
glob = "0.3.0"
|
glob = "0.3.0"
|
||||||
ring = "0.16.20"
|
ring = "0.16.20"
|
||||||
hotwatch = "0.4.5"
|
hotwatch = "0.4.5"
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
[config]
|
||||||
|
refresh_rate="1s"
|
||||||
|
|
||||||
[server]
|
[server]
|
||||||
address="127.0.0.1:8000"
|
address="127.0.0.1:8000"
|
||||||
#workers=4
|
#workers=4
|
||||||
|
|
|
@ -3,16 +3,27 @@ use crate::random::RandomURLConfig;
|
||||||
use byte_unit::Byte;
|
use byte_unit::Byte;
|
||||||
use config::{self, ConfigError};
|
use config::{self, ConfigError};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
/// Configuration values.
|
/// Configuration values.
|
||||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
/// Configuration settings.
|
||||||
|
pub config: Option<Settings>,
|
||||||
/// Server configuration.
|
/// Server configuration.
|
||||||
pub server: ServerConfig,
|
pub server: ServerConfig,
|
||||||
/// Paste configuration.
|
/// Paste configuration.
|
||||||
pub paste: PasteConfig,
|
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.
|
/// Server configuration.
|
||||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -21,11 +21,8 @@ async fn main() -> IoResult<()> {
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
let config_path =
|
let config_path =
|
||||||
PathBuf::from(env::var("CONFIG").unwrap_or_else(|_| String::from("config.toml")));
|
PathBuf::from(env::var("CONFIG").unwrap_or_else(|_| String::from("config.toml")));
|
||||||
let config = Arc::new(RwLock::new(
|
let config = Config::parse(&config_path).expect("failed to parse config");
|
||||||
Config::parse(&config_path).expect("failed to parse config"),
|
let server_config = config.server.clone();
|
||||||
));
|
|
||||||
let cloned_config = Arc::clone(&config);
|
|
||||||
let server_config = config.read().expect("cannot acquire config").server.clone();
|
|
||||||
|
|
||||||
// Create necessary directories.
|
// Create necessary directories.
|
||||||
fs::create_dir_all(&server_config.upload_path)?;
|
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.
|
// Set up a watcher for the configuration file changes.
|
||||||
let mut hotwatch = Hotwatch::new_with_custom_delay(Duration::from_secs(1))
|
let mut hotwatch = Hotwatch::new_with_custom_delay(
|
||||||
.expect("failed to initialize configuration file watcher");
|
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.
|
// Hot-reload the configuration file.
|
||||||
|
let config = Arc::new(RwLock::new(config));
|
||||||
|
let cloned_config = Arc::clone(&config);
|
||||||
let config_watcher = move |event: Event| {
|
let config_watcher = move |event: Event| {
|
||||||
if let Event::Write(path) = event {
|
if let Event::Write(path) = event {
|
||||||
match Config::parse(&path) {
|
match Config::parse(&path) {
|
||||||
|
|
Loading…
Reference in a new issue