Skip to content

Commit

Permalink
feat: initial support of .env files (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored Mar 28, 2024
1 parent ca74c91 commit eacdca9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ anyhow = "1.0.79"
async-trait = "0.1.77"
byte-unit = "5.1.4"
chrono = "0.4.31"
clap = { version = "4.4.18", features = ["derive", "env"] }
const_format = "0.2.32"
const-hex = "1.10.0"
csv = "1.3.0"
Expand All @@ -38,6 +37,10 @@ url = "2.5.0"
tokio = { version = "1.35.1", features = ["rt-multi-thread", "macros"] }
tokio-util = "0.7.10"

# config
clap = { version = "4.4.18", features = ["derive", "env"] }
dotenvy = "0.15.7"

# serialization
serde = "1.0.193"
serde_json = "1.0.108"
Expand Down
1 change: 1 addition & 0 deletions config/stratus.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHAIN_ID=2008
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,25 @@ pub fn init_global_services<T>() -> T
where
T: clap::Parser + WithCommonConfig + Debug,
{
// load .env files
let binary_full_path = std::env::current_exe().unwrap();
let mut binary = binary_full_path.file_name().unwrap().to_str().unwrap().to_owned();
if binary.starts_with("test_") {
binary = "test-int".to_string();
}
let env = std::env::var("ENV").unwrap_or_else(|_| "local".to_string());
let env_filename = format!("config/{}.env.{}", binary, env);

println!("Reading ENV file: {}", env_filename);
let _ = dotenvy::from_filename(env_filename);

// parse configuration
let config = T::parse();
println!("Parsed configuration: {:?}", config);

// init services
infra::init_tracing();
infra::init_metrics(config.common().metrics_histogram_kind);
tracing::info!(?config);

config
}

0 comments on commit eacdca9

Please sign in to comment.