Load Configuration
spring-rs implements a concise configuration loading function
You can define configuration in the following way:
use spring::config::Configurable;
use serde::Deserialize;
#[derive(Debug, Configurable, Deserialize)]
#[config_prefix = "my-plugin"]
struct Config {
a: u32,
b: bool,
}
The configuration in toml
can be read through the app.get_config()
method:
[my-plugin]
a = 10
b = true
use spring::async_trait;
use spring::plugin::Plugin;
use spring::config::Configurable;
use serde::Deserialize;
#[derive(Debug, Configurable, Deserialize)]
#[config_prefix = "my-plugin"]
struct Config {
a: u32,
b: bool,
}
struct MyPlugin;
#[async_trait]
impl Plugin for MyPlugin {
async fn build(&self, app: &mut AppBuilder) {
// Loading configuration in your own plugin
let config = app.get_config::<Config>().expect("load config failed");
// do something...
}
}
Use configuration in other plugins
Using environment variables in configuration files
spring-rs implements a simple interpolator.
You can use the ${ENV_VAR_NAME}
placeholder in the toml configuration file to read the value of the environment variable.
If the value does not exist, the placeholder is not replaced.
You can specify the default value of the placeholder using the ${ENV_VAR_NAME:default_value}
syntax.
[sea-orm]
uri = "${DATABASE_URL:postgres://postgres:xudjf23adj213@localhost/postgres}"
enable_logging = true