Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow overwrite config with ENV #46

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ ctor = "0.2.8"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
axum = "0.6.0"
dotenv = "0.15"
2 changes: 1 addition & 1 deletion examples/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async fn main() -> anyhow::Result<()> {
init_tracing();

let args = Args::parse();
let cfg: Config = Config::from_file(args.config_file)?;
let cfg: Config = Config::from_file_and_env(args.config_file)?;
let cloud_prover = CloudProver::new(
cfg.prover
.cloud
Expand Down
49 changes: 49 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prover::CircuitType;
use dotenv::dotenv;
use serde::{Deserialize, Serialize};
use serde_json;
use std::fs::File;
Expand Down Expand Up @@ -72,4 +73,52 @@ impl Config {
let file = File::open(file_name)?;
Config::from_reader(&file)
}

pub fn from_file_and_env(file_name: String) -> anyhow::Result<Self> {
let mut cfg = Config::from_file(file_name)?;
cfg.override_with_env()?;
Ok(cfg)
}

fn get_env_var(key: &str) -> anyhow::Result<Option<String>> {
Ok(std::env::var_os(key).map(|val| {
val.to_str()
.ok_or_else(|| anyhow::anyhow!("{key} env var is not valid UTF-8"))
.map(String::from)
}).transpose()?)
}

fn override_with_env(&mut self) -> anyhow::Result<()> {
dotenv().ok();

if let Some(val) = Self::get_env_var("PROVER_NAME_PREFIX")? {
self.prover_name_prefix = val;
}
if let Some(val) = Self::get_env_var("KEYS_DIR")? {
self.keys_dir = val;
}
if let Some(val) = Self::get_env_var("COORDINATOR_BASE_URL")? {
self.coordinator.base_url = val;
}
if let Some(val) = Self::get_env_var("L2GETH_ENDPOINT")? {
if let Some(l2geth) = &mut self.l2geth {
l2geth.endpoint = val;
}
}
if let Some(val) = Self::get_env_var("CIRCUIT_TYPE")? {
self.prover.circuit_type = CircuitType::from_u8(val.parse()?);
}
if let Some(val) = Self::get_env_var("PROVING_SERVICE_BASE_URL")? {
if let Some(cloud) = &mut self.prover.cloud {
cloud.base_url = val;
}
}
if let Some(val) = Self::get_env_var("PROVING_SERVICE_API_KEY")? {
if let Some(cloud) = &mut self.prover.cloud {
cloud.api_key = val;
}
}

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/prover/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum CircuitType {
}

impl CircuitType {
fn from_u8(v: u8) -> Self {
pub fn from_u8(v: u8) -> Self {
match v {
1 => CircuitType::Chunk,
2 => CircuitType::Batch,
Expand Down