Hexagonal Architecgture Configuration Service
A flexible, type-safe configuration management library for Rust applications, built with hexagonal architecture principles.
Features
- Multiple Configuration Sources: Environment variables, YAML files, command-line arguments, etcd, and Redis
- Type Safety: Automatic type conversions with comprehensive error handling
- Priority-Based Precedence: CLI arguments override environment variables, which override configuration files
- Dynamic Reloading: Watch configuration files, etcd, and Redis for changes and reload automatically
- Hexagonal Architecture: Clean separation of concerns with domain, ports, and adapters
- Extensible: Easy to implement custom configuration sources via traits
- Async Support: Built-in support for async remote sources (etcd, Redis)
Quick Start
Add this to your Cargo.toml:
[]
= "0.7.0"
Basic Usage
use *;
Feature Flags
The crate uses feature flags to enable optional functionality:
| Feature | Description | Default |
|---|---|---|
yaml |
YAML file support via serde_yaml | ✅ |
env |
Environment variable support | ✅ |
cli |
Command-line argument support | ✅ |
reload |
Dynamic reloading with file watching | ❌ |
etcd |
etcd remote configuration support | ❌ |
redis |
Redis remote configuration support | ❌ |
remote |
All remote sources (etcd + redis) | ❌ |
full |
All features | ❌ |
Custom Feature Configuration
[]
= { = "0.7.0", = false, = ["yaml", "env"] }
Architecture
This crate follows hexagonal architecture principles:
┌───────────────────────────────────────────────────┐
│ Application │
│ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Domain Layer │ │
│ │ │ │
│ │ • ConfigKey, ConfigValue (core types) │ │
│ │ • ConfigurationService (business logic) │ │
│ │ • ConfigError (error types) │ │
│ │ │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Ports Layer │ │
│ │ │ │
│ │ • ConfigSource trait (source interface) │ │
│ │ • ConfigWatcher trait (watcher interface) │ │
│ │ • ConfigParser trait (parser interface) │ │
│ │ │ │
│ └─────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Adapters Layer │ │
│ │ │ │
│ │ • YamlFileAdapter │ │
│ │ • EnvVarAdapter │ │
│ │ • CommandLineAdapter │ │
│ │ • EtcdAdapter │ │
│ │ • RedisAdapter │ │
│ │ • FileWatcher, EtcdWatcher, RedisWatcher │ │
│ │ │ │
│ └─────────────────────────────────────────────┘ │
└───────────────────────────────────────────────────┘
String Convenience Methods
For more ergonomic usage, the crate provides _str variants of common methods that accept string slices directly:
use *;
Both approaches work - use whichever feels more natural for your code style!
Examples
Multiple Configuration Sources
Combine multiple sources with automatic precedence handling:
use *;
Dynamic Configuration Reloading
Watch configuration files for changes:
use *;
use ;
Type Conversions
Automatic type conversion with error handling:
use *;
Environment Variable Prefix Filtering
Filter environment variables by prefix:
use *;
Remote Configuration (etcd)
use *;
async
Remote Configuration (Redis)
use *;
async
Watching Remote Configuration Changes
etcd Watcher
Watch for configuration changes in etcd using its native watch API:
use *;
use EtcdWatcher;
use ConfigWatcher;
use ;
async
Redis Watcher
Watch for configuration changes in Redis using keyspace notifications:
use *;
use RedisWatcher;
use ConfigWatcher;
use ;
Note: Redis keyspace notifications must be enabled on the Redis server:
# Via redis-cli
# Or in redis.conf
Custom Configuration Sources
Implement the ConfigSource trait to create custom sources:
use ConfigSource;
use ;
;
Priority System
Configuration sources have priorities that determine precedence:
| Priority | Source | Description |
|---|---|---|
| 3 | CLI Arguments | Highest priority, overrides all others |
| 2 | Environment Variables | Overrides files and remote sources |
| 1 | Files & Remote | YAML, etcd, Redis - lowest priority |
When multiple sources provide the same key, the value from the highest priority source is used.
Error Handling
The crate provides comprehensive error types via thiserror:
use *;
Running Examples
The crate includes several examples:
# Basic usage with environment variables
# String convenience methods
# Multiple sources with precedence
# Dynamic reloading
Testing
Run tests with different feature combinations:
# Run all tests with default features
# Run tests with all features
# Run tests with specific features
# Run property-based tests
Remote Watcher Integration Tests
Integration tests for etcd and Redis watchers are included in their respective integration test files and use Docker containers automatically via testcontainers-rs. These tests will automatically skip if Docker is not available:
# Run all Redis tests (including watcher tests)
# Run all etcd tests (including watcher tests)
# Run specific watcher tests
Docker must be installed and running for these tests to execute. If Docker is unavailable, the tests will be skipped with a warning message.
Documentation
Generate and view the full API documentation:
Contributing
Contributions are welcome! Please ensure:
- All tests pass:
cargo test --all-features - Code is formatted:
cargo fmt --check - No clippy warnings:
cargo clippy --all-features - Documentation is updated for public APIs
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Project Status
This crate is currently at version 1.0.0 and includes:
- ✅ Core domain types (ConfigKey, ConfigValue)
- ✅ Configuration service with priority-based source management
- ✅ YAML file support
- ✅ Environment variable support
- ✅ Command-line argument support
- ✅ Dynamic reloading with file watching
- ✅ etcd integration
- ✅ Redis integration
- ✅ Comprehensive test suite (unit, integration, property-based)
- ✅ Full API documentation
- ✅ Example code
Acknowledgments
This crate was designed with inspiration from configuration management libraries in other ecosystems, adapted to Rust's ownership model and type system.