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

``rust //DeserializeandClone` traits should be applied

[derive(Clone, Debug, Deserialize)]

struct Entry { ... } ```

```rust

[test]

fn basetest() { // Panic if no such file config/config.yaml let config: Config = Config::fromfile("config/config.yaml"); let entry: &Entry = config.asinner(); // borrowed value has the same lifetimes as config let entry: Entry = config.toinner(); // clone a new Entry let entry: Entry = config.into_inner(); // take ownership } ```

```rust

[tokio::test]

async fn dynamictest() { // Create a dynamic config and a watcher let (config, mut watcher) = DynamicConfig::::watchfile("config/config.yaml"); // Listen to file modify event watcher.watch().unwrap(); let lock = config.lock(); let entry: &Entry = lock.asinner(); // borrow Entry let entry: Entry = lock.toinner(); // clone a new Entry // let entry: Entry = lock.intoinner(); panic! cannot take the lock ownership let arc = config.asarc(); // clone a new arc // Stop watching watcher.stop().unwrap(); // You can watch twice watcher.watch().unwrap(); } ```

```rust 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() .appid("test") .namespace("test", ConfigType::YAML) .serverurl("http://localhost:8080") .finish(); let (config, mut watcher) = DynamicConfig::::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());
}

}

```