kosei

Crates.iokosei
lib.rskosei
version0.2.0
sourcesrc
created_at2022-12-16 10:39:14.603376
updated_at2023-02-25 14:43:01.403397
descriptionA easy-to-use configuration crate with the Rust programming language.
homepage
repositoryhttps://github.com/iGxnon/kosei
max_upload_size
id738670
size74,941
iGxnon (iGxnon)

documentation

README

Kōsei

こうせい

A easy-to-use configuration crate with the Rust programming language.

Features

Crates.io Crates.io

Features

dynamic hot-reload config support
apollo Apollo support
nacos Nacos support

Quickstart

See examples for further use.

Config Entry

// `Deserialize` and `Clone` traits should be applied
#[derive(Clone, Debug, Deserialize)]
struct Entry {
    ...
}
  • Base file config
#[test]
fn base_test() {
    // Panic if no such file `config/config.yaml`
    let config: Config<Entry> = Config::from_file("config/config.yaml");
    let entry: &Entry = config.as_inner(); // borrowed value has the same lifetimes as config
    let entry: Entry = config.to_inner();  // clone a new Entry
    let entry: Entry = config.into_inner(); // take ownership
}
  • Dynamic file config
#[tokio::test]
async fn dynamic_test() {
    // Create a dynamic config and a watcher
    let (config, mut watcher) = DynamicConfig::<Entry>::watch_file("config/config.yaml");
    // Listen to file modify event
    watcher.watch().unwrap();
    let lock = config.lock();
    let entry: &Entry = lock.as_inner();  // borrow Entry
    let entry: Entry = lock.to_inner();  // clone a new Entry
    // let entry: Entry = lock.into_inner(); panic! cannot take the lock ownership
    let arc = config.as_arc();  // clone a new arc
    // Stop watching
    watcher.stop().unwrap();
    // You can watch twice
    watcher.watch().unwrap();
}
  • Dynamic Apollo config
use kosei::apollo::{Builder, WatchMode};
use kosei::{ConfigType, DynamicConfig, InnerWatcher};
use serde::Deserialize;
use std::time::Duration;

#[derive(Deserialize, Clone, Debug)]
struct Entry {
    x: f64,
    y: f64,
}

#[tokio::main]
async fn main() {
    let client = Builder::new()
        .app_id("test")
        .namespace("test", ConfigType::YAML)
        .server_url("http://localhost:8080")
        .finish();
    let (config, mut watcher) =
        DynamicConfig::<Entry>::watch_apollo(client, WatchMode::RealTime).await;

    watcher.watch().unwrap();

    {
        let guard = config.lock();
        println!("entry: {:?}", guard.as_inner());
    }

    tokio::time::sleep(Duration::from_secs(10)).await;

    {
        let guard = config.lock();
        println!("entry: {:?}", guard.as_inner());
    }
}


Commit count: 13

cargo fmt