#reload #notify #watch #json-toml #json-configuration #config-file

reloadify

A library for automatically reloading configuration files

2 releases

0.1.1 Jul 10, 2024
0.1.0 Jul 7, 2024

#221 in Configuration

46 downloads per month

MIT license

14KB
184 lines

Reloadify 🔁

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.

Features ✨

  • Automatic Reloading: Detects changes in configuration files and automatically reloads them.
  • Supports Multiple Formats: Works with JSON, TOML, XML, and more.
  • Easy Integration: Designed for seamless integration into Rust applications.
  • Customizable: Allows customization of file watching strategies and reload behaviors.
  • Live Changes: Returns a configuration receiving channel. When the configuration changes, the caller will receive latest configuration.

Installation 🚀

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" }

Usage 🛠️

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(())
}

Documentation 📚

For detailed usage instructions and API reference, see the documentation.

Contributing 🤝

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

License 📝

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~1–12MB
~94K SLoC