Crate redis_config

source ·
Expand description

Docs

§redis-config

Crates.io Rust Crates.io Docs.rs CI codecov

Implementation of Redis source as Async source for config-rs crate.

redis-config extends the list of possible sources provided by config-rs and provides an asynchronous RedisSource source using the redis-rs.

RedisSource supports reading configuration:

  • from Hash using the HGETALL command,
  • from String using the GET command,
§Features

There are a few features defined in redis-rs that can enable additional functionality if so desired. Some of them are turned on by default.

  • ahash: enables ahash map/set support & uses ahash internally (+7-10% performance) (optional)
  • tokio-comp: enables support for tokio runtime (enabled by default)
  • async-std-comp: enables support for async-std runtime (optional)
§Tls features
  • async-std-native-tls-comp: enables support for native-tls for async-std (optional)
  • async-std-rustls-comp: enables support for rustls for async-std (optional)
  • tokio-native-tls-comp: enables support for native-tls for tokio (optional)
  • tokio-rustls-comp: enables support for rustls for tokio (optional).

See the examples for general usage information.

§Usage

§Dependencies

# Cargo.toml

[dependencies]
config = "0.13.3"
redis_config = { version = "*", features = ["tokio-comp"]}
tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] }
serde = { version = "1.0", features = ["derive"]}

§Usage example

 use config::builder::AsyncState;
 use redis_config::{states, RedisSource};

 // hardcoded values, shouldn't be in production
 const REDIS_URL: &str = "redis://127.0.0.1:6379";
 const SOURCE_KEY: &str = "application-settings";

 #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Clone)]
 struct ServerSettings {
     ttl: i64,
     path: String,
     // another settings
     // ...
 }

 #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
 struct DbSettings {
     pool_size: usize,
     // another settings
     // ...
 }

 #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
 struct ApplicationSettings {
     // settings that will be taken from RedisSource
     server: ServerSettings,
     // settings that will be taken from Env
     db: DbSettings,
 }

 async fn get_config() -> Result<ApplicationSettings, config::ConfigError> {
     let config = config::ConfigBuilder::<AsyncState>::default()
         .add_source(
             config::Environment::with_prefix("APP")
                 .separator("__")
                 .try_parsing(true),
         )
         .add_async_source(
             RedisSource::<_, states::PlainString>::try_new(SOURCE_KEY, REDIS_URL)
                 .map_err(|err| config::ConfigError::NotFound(err.to_string()))?,
         )
         .build()
         .await?;

     config.try_deserialize()
 }

 #[tokio::main]
 async fn main() {
     let config = get_config().await.unwrap();
 }

§More

See the documentation for more usage information.

§License

redis_config is primarily distributed under the terms of both the MIT license.

See LICENSE for details.

Modules§

Structs§

  • A configuration async source backed up by a Redis.

Enums§

  • Represents all possible errors that can occur during working with the source.

Type Aliases§