Skip to main content

config_loading/
config_loading.rs

1//! Example: Loading Layered Configuration
2//!
3//! This example demonstrates how to load configuration from multiple sources
4//! using Velora's layered configuration system.
5//!
6//! Run with:
7//! ```bash
8//! cargo run --example config_loading
9//! ```
10
11use velora_core::VeloraConfig;
12
13fn main() -> Result<(), Box<dyn std::error::Error>> {
14    println!("=== Velora Configuration Loading Examples ===\n");
15
16    // Example 1: Load from single file
17    println!("1. Loading from single file (base.toml):");
18    match VeloraConfig::from_file("config/base.toml") {
19        Ok(config) => {
20            println!("   ✓ Loaded successfully");
21            println!("   Database backend: {:?}", config.database.backend);
22            println!("   Logging level: {}", config.logging.level);
23            println!();
24        }
25        Err(e) => {
26            println!("   ✗ Failed to load: {e}");
27            println!("   (This is expected if config/base.toml doesn't exist)\n");
28        }
29    }
30
31    // Example 2: Load layered configuration (base + testing)
32    println!("2. Loading layered config (base.toml + testing.toml):");
33    match VeloraConfig::from_files(&["config/base.toml", "config/testing.toml"]) {
34        Ok(config) => {
35            println!("   ✓ Loaded successfully");
36            println!("   Database backend: {:?}", config.database.backend);
37            println!("   Logging level: {}", config.logging.level);
38            println!("   Max position size: {}", config.risk.max_position_size);
39            println!();
40        }
41        Err(e) => {
42            println!("   ✗ Failed to load: {e}");
43            println!("   (This is expected if config files don't exist)\n");
44        }
45    }
46
47    // Example 3: Load with optional local overrides
48    println!("3. Loading with optional local.toml override:");
49    match VeloraConfig::from_files_optional(&[
50        ("config/base.toml", true),    // required
51        ("config/testing.toml", true), // required
52        ("config/local.toml", false),  // optional (won't error if missing)
53    ]) {
54        Ok(config) => {
55            println!("   ✓ Loaded successfully");
56            println!("   Database backend: {:?}", config.database.backend);
57            println!();
58        }
59        Err(e) => {
60            println!("   ✗ Failed to load: {e}");
61            println!();
62        }
63    }
64
65    // Example 4: Environment variable overrides
66    println!("4. Environment variable override example:");
67    println!("   Set: export VELORA_LOGGING_LEVEL=trace");
68    println!("   Then the logging level will override any config file value");
69    println!();
70
71    // Example 5: Demonstrate different environments
72    println!("5. Different environment configurations:\n");
73
74    let environments = vec![
75        ("Testing", vec!["config/base.toml", "config/testing.toml"]),
76        (
77            "Paper Trading",
78            vec!["config/base.toml", "config/paper-trading.toml"],
79        ),
80        (
81            "Live Trading",
82            vec!["config/base.toml", "config/live-trading.toml"],
83        ),
84        (
85            "Backtesting",
86            vec!["config/base.toml", "config/backtesting.toml"],
87        ),
88    ];
89
90    for (name, paths) in environments {
91        print!("   {name}: ");
92        match VeloraConfig::from_files(&paths) {
93            Ok(config) => {
94                println!(
95                    "DB={:?}, DryRun={}, MaxPos=${}",
96                    config.database.backend,
97                    config.engine.live.dry_run,
98                    config.risk.max_position_size
99                );
100            }
101            Err(_) => {
102                println!("(config files not found)");
103            }
104        }
105    }
106
107    println!("\n=== Configuration Priority ===");
108    println!("Highest → Lowest:");
109    println!("1. Environment variables (VELORA_*)");
110    println!("2. Last config file in list");
111    println!("3. ...");
112    println!("4. First config file in list (usually base.toml)");
113    println!("5. Default values in code");
114
115    println!("\n=== Usage Examples ===");
116    println!("Development:");
117    println!("  cargo run -- --config config/base.toml --config config/testing.toml");
118    println!();
119    println!("Paper Trading:");
120    println!("  cargo run -- --config config/base.toml --config config/paper-trading.toml");
121    println!();
122    println!("Live Trading (⚠️  REAL MONEY):");
123    println!("  export VELORA_EXCHANGES_BINANCE_PROD_API_KEY=your_key");
124    println!("  export VELORA_EXCHANGES_BINANCE_PROD_API_SECRET=your_secret");
125    println!("  cargo run -- --config config/base.toml --config config/live-trading.toml");
126
127    Ok(())
128}