Skip to content

Commit

Permalink
Use Environment Variables instead of CLI Arguments (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
37ng authored Dec 22, 2023
1 parent 19d0af0 commit 02adc57
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ sha3 = "0.10"
regex = "1"
rustc-hex= "2.1"
peg = "0.8"
dotenvy = "0.15.7"
envy = "0.4.2"

[dev-dependencies]
jsonrpsee = { version = "0.21", features = ["macros", "server", "client"] }
Expand Down
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ADDRESS="127.0.0.1:9944"
PROVIDER="http://127.0.0.1:8545"
39 changes: 29 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ pub mod types;
mod util;

use std::str::FromStr;
use serde::Deserialize;

use anyhow::Result;
use argh::FromArgs;
use ethers::types::Address;
use jsonrpsee::server::Server;

Expand All @@ -97,34 +97,42 @@ pub use crate::{
rpc::DidRegistryServer,
};

const DEFAULT_ADDRESS: &str = "127.0.0.1:9944";
const DEFAULT_PROVIDER: &str = "http://127.0.0.1:8545";

// TODO: Get registry address from environment variable, or configuration file
// in order to support multiple chains, we may need to support multiple providers via RPC
// so it could be worth defining a config file that maps chainId to RPC provider (like
// did-eth-resolver)
/// The address of the DID Registry contract on the Ethereum Sepolia Testnet
pub const DID_ETH_REGISTRY: &str = "0xd1D374DDE031075157fDb64536eF5cC13Ae75000";

#[derive(FromArgs)]
#[derive(Deserialize)]
/// DID Ethereum Resolver XMTP Gateway
struct DidEthGatewayApp {
/// the address to start the server
#[argh(option, short = 'a', default = "String::from(\"127.0.0.1:9944\")")]
#[serde(default= "default_address")]
address: String,

/// ethereum RPC Provider
#[argh(
option,
short = 'p',
default = "String::from(\"wss://eth.llamarpc.com\")"
)]
#[serde(default= "default_provider")]
provider: String,
}

fn default_address() -> String {
DEFAULT_ADDRESS.to_string()
}

fn default_provider() -> String {
DEFAULT_PROVIDER.to_string()
}

/// Entrypoint for the did:ethr Gateway
pub async fn run() -> Result<()> {
crate::util::init_logging();
let opts: DidEthGatewayApp = argh::from_env();

dotenvy::dotenv()?;
let opts: DidEthGatewayApp = envy::from_env()?;

let server = Server::builder().build(opts.address).await?;
let addr = server.local_addr()?;
let registry_address = Address::from_str(DID_ETH_REGISTRY)?;
Expand All @@ -135,3 +143,14 @@ pub async fn run() -> Result<()> {
handle.stopped().await;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn internal() {
assert_eq!(DEFAULT_ADDRESS, default_address());
assert_eq!(DEFAULT_PROVIDER, default_provider());
}
}

0 comments on commit 02adc57

Please sign in to comment.