Distributed Config
A robust configuration management library for Rust applications running in distributed environments.
distributed-config provides a unified interface for loading, accessing, and synchronizing configuration across multiple nodes with support for dynamic updates, validation, and various backend stores.
โจ Features
- ๐๏ธ Hierarchical Configuration: Organize configuration in a tree structure with dot-notation access
- ๐ Multiple Sources: Load from files (JSON, YAML, TOML), environment variables, and remote HTTP endpoints
- ๐ Dynamic Updates: Real-time configuration changes with change notifications and watchers
- โ Schema Validation: Strong typing and JSON Schema validation for configuration safety
- ๐ Distributed Sync: Synchronize configuration across multiple nodes (with optional backends)
- ๐ฉ Feature Flags: Built-in feature flag management with real-time toggling
- ๐ Versioning: Configuration history and rollback support
- ๐ Secure: Safe handling of sensitive configuration values
- โก Async First: Built on Tokio with async/await throughout
- ๐ฏ Type Safe: Leverage Rust's type system for configuration validation
๐ Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["full"] }
= { = "1.0", = ["derive"] }
Basic Usage
use ;
use ;
async
Advanced Usage with Multiple Sources
use ;
use ;
use Duration;
async
๐ Configuration Sources
File Source
Supports JSON, YAML, and TOML formats:
let file_source = new
.add_file // Load into root
.add_file // Load into "database" namespace
.add_optional_file; // Optional file (won't fail if missing)
Environment Source
Maps environment variables to configuration keys:
let env_source = new
.prefix // Only variables starting with MYAPP_
.separator // MYAPP_DATABASE__HOST -> database.host
.case_sensitive; // Convert to lowercase
Examples:
MYAPP_DATABASE__HOST=localhostโdatabase.host = "localhost"MYAPP_SERVER__PORT=8080โserver.port = 8080MYAPP_DEBUG=trueโdebug = true
Remote Source
Load configuration from HTTP endpoints:
let remote_source = new
.endpoint
.auth_token
.timeout
.poll_interval // Check for updates every 30s
.header;
โ Validation
Use JSON Schema validation to ensure configuration correctness:
use ;
let validator = new
.add_schema_from_json?
.add_schema_from_json?
.add_schema_from_string?;
config.set_validator;
Built-in schemas available:
schemas::database_config()- Database connection settingsschemas::server_config()- Server/HTTP settingsschemas::api_config()- API client settingsschemas::feature_flags()- Feature flag definitions
๐ Dynamic Updates & Watching
Monitor configuration changes in real-time:
// Watch specific keys
let mut db_watcher = config.watch.await?;
let mut feature_watcher = config.watch.await?;
spawn;
// Watch with patterns
let mut all_watcher = config.watch.await?; // Watch everything
let mut app_watcher = config.watch.await?; // Watch app.* keys
Update configuration at runtime:
// Update individual values
config.set_value.await?;
config.set_value.await?;
// Cluster-wide updates (when using distributed backends)
config.set_value_for_cluster.await?;
๐ฉ Feature Flags
Built-in feature flag support:
// Check feature flags
if config.is_feature_enabled?
if config.is_feature_enabled?
// Toggle feature flags
config.set_value.await?;
// Node-specific feature flags
let node_id = "node-1";
if config.get_value_for_node?.as_bool?
๐ History & Versioning
Track configuration changes over time:
// Get change history
let history = config.get_history.await?;
for entry in history
// Save current configuration snapshot
config.save_to_file.await?;
๐ Distributed Backends (Optional)
Enable distributed configuration synchronization:
[]
= { = "0.1", = ["redis-backend"] }
# or
= { = "0.1", = ["etcd-backend"] }
# or
= { = "0.1", = ["all-backends"] }
๐ง Configuration File Examples
YAML Configuration
# config.yaml
app:
name: "My Application"
version: "1.0.0"
server:
host: "0.0.0.0"
port: 8080
workers: 4
debug: false
database:
host: "localhost"
port: 5432
username: "myuser"
password: "mypass"
database: "mydb"
max_connections: 10
timeout: 30
feature_flags:
new_ui: true
beta_features: false
experimental_cache: true
cache:
ttl: 3600
max_size: 1000
JSON Configuration
๐ Examples
Run the examples to see the library in action:
# Basic usage example
# Distributed synchronization example
# Feature flags example
๐งช Testing
Run the test suite:
# Run all tests
# Run tests with logging
RUST_LOG=debug
# Run specific test module
# Run integration tests
๐ค Contributing
Contributions are welcome! Please read our Contributing Guide for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Ensure tests pass (
cargo test) - Commit your changes (
git commit -am 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is 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.
๐ Acknowledgments
- Built with Tokio for async runtime
- Uses serde for serialization
- Configuration watching powered by notify
- JSON Schema validation via jsonschema
- HTTP client using reqwest
Happy configuring! ๐๏ธ