8 releases

0.0.9 Sep 4, 2024
0.0.8 Aug 25, 2024

#688 in Configuration

Download history 261/week @ 2024-08-07 204/week @ 2024-08-14 373/week @ 2024-08-21 74/week @ 2024-08-28 199/week @ 2024-09-04 11/week @ 2024-09-11 5/week @ 2024-09-18 51/week @ 2024-09-25 56/week @ 2024-10-02 5/week @ 2024-10-09

118 downloads per month
Used in spring-actuator

MIT license

35KB
793 lines

crates.io Documentation

Introduction

spring-boot is the core module of the spring project, which includes: configuration management, plugin management, and component management.

All plugins need to implement the Plugin feature.

How to write your own plugin

Add dependencies

spring-boot = { version = "0.0.9" }      # This crate contains the definition of plugin traits
serde = { workspace = true, features = ["derive"] } # Used to parse plugin configuration items
use serde::Deserialize;
use spring_boot::async_trait;
use spring_boot::config::Configurable;
use spring_boot::{app::AppBuilder, plugin::Plugin};

struct MyPlugin;

#[async_trait]
impl Plugin for MyPlugin {
    async fn build(&self, app: &mut AppBuilder) {
        // Call app.get_config::<Config>(self) method to get configuration items
        match app.get_config::<Config>(self) {
            Ok(config) => {
                println!("{:#?}", config);
                assert_eq!(config.a, 1);
                assert_eq!(config.b, true);

                // Get the configuration items to build the corresponding components

            }
            Err(e) => println!("{:?}", e),
        }
    }
}

/// Configuration item prefix
impl Configurable for MyPlugin {
    fn config_prefix(&self) -> &str {
        "my-plugin"
    }
}

/// Plugin configuration
#[derive(Debug, Deserialize)]
struct Config {
    a: u32,
    b: bool,
}

You can use the derive macro to implement the Configurable trait:

/// Use the `config_prefix` attr macro to define the prefix configured in the toml file
#[derive(Configurable)]
#[config_prefix = "my-plugin"]
struct MyPlugin;

For the complete code, refer to plugin-example, or refer to other built-in plugin codes.

Dependencies

~12–19MB
~254K SLoC