From 3359ee9643c9a3ce7e5467721c306d7d81cfad93 Mon Sep 17 00:00:00 2001 From: ABresting Date: Sat, 14 Feb 2026 17:10:49 +0100 Subject: [PATCH 1/2] add dotenv support with auto-load and --env-file flag (#13) --- .env.example | 3 +++ .gitignore | 3 +++ setu-validator/src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..50e4c6c --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +# PoCW Economics +POCW_ENABLED=false +POCW_TRANSFER_FEE=21000 diff --git a/.gitignore b/.gitignore index f10e9e5..5b84d57 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,9 @@ Thumbs.db **/*.rs.bk Cargo.lock +# Environment +.env + # Logs *.log logs/ diff --git a/setu-validator/src/main.rs b/setu-validator/src/main.rs index 8a5f4b2..7ebf8cd 100644 --- a/setu-validator/src/main.rs +++ b/setu-validator/src/main.rs @@ -94,8 +94,51 @@ fn load_key_info(key_file: &str) -> anyhow::Result<(String, Vec, Vec)> { Ok((account_address, public_key, signature)) } +/// Load environment variables from a dotenv file. +/// Priority: `--env-file ` flag, then `.env` in the working directory. +/// Each line should be KEY=VALUE (comments and blank lines are skipped). +fn load_env_file() { + let args: Vec = std::env::args().collect(); + let explicit = args.windows(2) + .find(|w| w[0] == "--env-file") + .map(|w| w[1].clone()); + + let path = match explicit { + Some(p) => p, + None => { + // Auto-load .env from working directory if it exists + if std::path::Path::new(".env").exists() { + ".env".to_string() + } else { + return; + } + } + }; + + let content = match std::fs::read_to_string(&path) { + Ok(c) => c, + Err(e) => { + eprintln!("Failed to read env file {}: {}", path, e); + std::process::exit(1); + } + }; + + for line in content.lines() { + let line = line.trim(); + if line.is_empty() || line.starts_with('#') { + continue; + } + if let Some((key, value)) = line.split_once('=') { + std::env::set_var(key.trim(), value.trim()); + } + } +} + #[tokio::main] async fn main() -> anyhow::Result<()> { + // Load .env automatically, or from --env-file + load_env_file(); + // Initialize tracing with more detailed output tracing_subscriber::fmt() .with_max_level(Level::DEBUG) From 7e4c069746e6e55adf1653e6636a568d1fe7bdd1 Mon Sep 17 00:00:00 2001 From: ABresting Date: Sat, 14 Feb 2026 17:28:31 +0100 Subject: [PATCH 2/2] docs: add --env-file usage to READMEs (#13) --- README.md | 10 +++++++--- setu-validator/README.md | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 204e4a5..f1789bc 100644 --- a/README.md +++ b/README.md @@ -168,13 +168,17 @@ cargo test --all #### 1. Start Validator ```bash -# Set environment variables +# Using an env file (auto-loads .env from working directory) +./target/release/setu-validator + +# Or specify a custom env file +./target/release/setu-validator --env-file config/prod.env + +# Or set environment variables directly export VALIDATOR_ID=validator-1 export VALIDATOR_HTTP_PORT=8080 export VALIDATOR_P2P_PORT=9000 export VALIDATOR_DB_PATH=/tmp/setu/validator - -# Start validator ./target/release/setu-validator ``` diff --git a/setu-validator/README.md b/setu-validator/README.md index 9a76fea..7c3e551 100644 --- a/setu-validator/README.md +++ b/setu-validator/README.md @@ -50,7 +50,10 @@ Validator node for the Setu network. Responsible for verifying events, maintaini # Start validator with default config cargo run -p setu-validator -# With environment variables +# With an env file (auto-loads .env from working directory, or use --env-file) +cargo run -p setu-validator -- --env-file .env + +# With inline environment variables VALIDATOR_ID=validator_1 \ VALIDATOR_PORT=8001 \ IS_LEADER=true \