Files
rustytorch/crates/production/rtx-config/src/lib.rs
T
2026-03-04 00:08:42 +00:00

133 lines
4.4 KiB
Rust

//! # RTX Config - Configuration Management System
//!
//! RTX Config provides comprehensive configuration management for RustyTorch
//! production deployments, including hot-reloading, secret management,
//! environment-specific configuration, and feature flags.
//!
//! ## Core Features
//!
//! - Hot-reloading configuration from files and environment variables
//! - Secure secret management with encryption at rest
//! - Environment-specific configuration with inheritance
//! - Feature flags and A/B testing support
//! - Configuration validation with JSON Schema
//! - Remote configuration fetching with caching
//! - Audit logging for configuration changes
#![deny(clippy::unwrap_used)]
#![cfg_attr(test, allow(clippy::unwrap_used))]
pub mod config;
pub mod environment;
pub mod error;
pub mod features;
pub mod loader;
pub mod secrets;
pub mod validation;
pub mod watcher;
pub use config::{ConfigManager, ConfigSource, ConfigValue, RuntimeConfig};
pub use environment::{Environment, EnvironmentConfig, EnvironmentManager};
pub use error::{ConfigError, ConfigResult};
pub use features::{FeatureFlag, FeatureFlagManager, FeatureState, VariantConfig};
pub use loader::{ConfigFormat, ConfigLoader, LoaderConfig};
pub use secrets::{EncryptionConfig, SecretManager, SecretStore, SecretValue};
pub use validation::{ConfigValidator, ValidationRule, ValidationSchema};
pub use watcher::{ConfigWatcher, WatchEvent, WatcherConfig};
use std::sync::Arc;
/// Convenience type for shared configuration manager
pub type SharedConfigManager = Arc<ConfigManager>;
/// Create a new configuration manager with default settings
pub async fn create_config_manager() -> ConfigResult<ConfigManager> {
// Add default configuration sources
ConfigManager::builder()
.add_source(ConfigSource::Environment)
.add_source(ConfigSource::File {
path: "config.toml".into(),
format: ConfigFormat::Toml,
required: false,
})
.enable_hot_reload(true)
.enable_validation(true)
.build()
.await
}
/// Create a production-ready configuration manager
pub async fn create_production_config_manager() -> ConfigResult<ConfigManager> {
// Production configuration sources (order matters - later sources override earlier ones)
ConfigManager::builder()
.add_source(ConfigSource::File {
path: "/etc/rtx/config.yaml".into(),
format: ConfigFormat::Yaml,
required: false,
})
.add_source(ConfigSource::File {
path: "./config/production.yaml".into(),
format: ConfigFormat::Yaml,
required: false,
})
.add_source(ConfigSource::Environment)
.add_source(ConfigSource::Remote {
url: std::env::var("RTX_CONFIG_URL").ok(),
headers: std::collections::HashMap::new(),
timeout: std::time::Duration::from_secs(10),
})
.enable_hot_reload(true)
.enable_validation(true)
.enable_secret_management(true)
.enable_feature_flags(true)
.build()
.await
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::NamedTempFile;
use tokio;
#[tokio::test]
async fn test_create_config_manager() {
let manager = create_config_manager().await.unwrap();
assert!(manager.is_hot_reload_enabled());
}
#[tokio::test]
async fn test_basic_config_operations() {
let manager = create_config_manager().await.unwrap();
// Test setting and getting configuration values
manager
.set("test.key", ConfigValue::String("test_value".to_string()))
.await
.unwrap();
let value = manager.get::<String>("test.key").await.unwrap();
assert_eq!(value, "test_value");
}
#[tokio::test]
async fn test_config_with_file_source() {
use std::io::Write;
let mut temp_file = NamedTempFile::new().unwrap();
writeln!(temp_file, "test_key = \"test_value\"").unwrap();
let manager = ConfigManager::builder()
.add_source(ConfigSource::File {
path: temp_file.path().to_path_buf(),
format: ConfigFormat::Toml,
required: true,
})
.build()
.await
.unwrap();
let value = manager.get::<String>("test_key").await.unwrap();
assert_eq!(value, "test_value");
}
}