#redis #configuration #settings

redis_config

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

5 releases

0.2.2 Mar 25, 2024
0.2.1 Jan 5, 2024
0.1.1 Dec 30, 2023
0.1.0 Oct 10, 2023
0.0.0-beta-1 Oct 7, 2023

#118 in Configuration

Download history 8/week @ 2023-12-29 16/week @ 2024-01-05 34/week @ 2024-02-23 50/week @ 2024-03-01 130/week @ 2024-03-22 23/week @ 2024-03-29 2/week @ 2024-04-05

155 downloads per month

MIT license

20KB
110 lines

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.

Dependencies

~8–24MB
~348K SLoC