Crates.io | reloadify |
lib.rs | reloadify |
version | 0.1.2 |
source | src |
created_at | 2024-07-07 11:10:58.713028 |
updated_at | 2024-09-20 02:21:09.853457 |
description | A library for automatically reloading configuration files |
homepage | |
repository | https://github.com/trayvonpan/reloadify/ |
max_upload_size | |
id | 1294741 |
size | 35,515 |
Reloadify is a Rust library designed to facilitate automatic reloading of configuration files in applications. It simplifies the process of detecting changes in configuration files (such as JSON, TOML, XML, etc.) and automatically applying those changes without requiring application restarts.
To use Reloadify in your Rust project, simply add it to your Cargo.toml
:
[dependencies]
reloadify = "0.1"
If you want to use the latest version, you can import it like this:
[dependencies]
reloadify = { git = "ssh://git@github.com/trayvonpan/reloadify.git", branch = "main" }
Here's a basic example demonstrating how to use Reloadify to automatically reload a JSON configuration file:
use reloadify::{ConfigId, Format, ReloadableConfig, Reloadify};
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, str::FromStr, time::Duration};
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TsConfig {
pub extends: String,
#[serde(rename = "compilerOptions")]
pub compiler_options: CompilerOptions,
pub files: Vec<String>,
pub include: Vec<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompilerOptions {
#[serde(rename = "outDir")]
pub out_dir: String,
pub types: Vec<String>,
}
const TS_CONFIG_ID: &str = "tsconfig";
fn main() -> Result<(), Box<dyn std::error::Error>> {
let reloadify = Reloadify::new();
let rx = reloadify.add::<TsConfig>(ReloadableConfig {
id: ConfigId::new(TS_CONFIG_ID),
path: PathBuf::from_str("examples/config/tsconfig.spec.json")?,
format: Format::Json,
poll_interval: Duration::from_secs(1),
})?;
// Optional: Spawn a thread to listen for the latest configuration.
std::thread::spawn(move || {
for latest_cfg in rx {
// Do something with the latest configuration...
println!("Received latest config: {:?}", latest_cfg);
}
});
let ts_config = reloadify.get::<TsConfig>(ConfigId::new(TS_CONFIG_ID))?;
// Do something with ts_config...
println!("{:?}", ts_config);
Ok(())
}
For detailed usage instructions and API reference, see the documentation.
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
This project is licensed under the MIT License - see the LICENSE file for details.