From b79a050fd4066391fd0dc413bd67283944ecb93b Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 5 May 2022 21:25:40 +0000 Subject: [PATCH] use argh for config --- web3-proxy/src/config.rs | 26 +++++++++++++++++--------- web3-proxy/src/main.rs | 17 ++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/web3-proxy/src/config.rs b/web3-proxy/src/config.rs index 8e391266..a377d158 100644 --- a/web3-proxy/src/config.rs +++ b/web3-proxy/src/config.rs @@ -1,3 +1,4 @@ +use argh::FromArgs; use governor::clock::QuantaClock; use serde::Deserialize; use std::collections::{BTreeMap, HashMap}; @@ -6,17 +7,24 @@ use std::sync::Arc; use crate::connection::Web3Connection; use crate::Web3ProxyApp; -#[derive(Deserialize)] -pub struct RootConfig { - pub config: Web3ProxyConfig, - // BTreeMap so that iterating keeps the same order - pub balanced_rpc_tiers: BTreeMap>, - pub private_rpcs: HashMap, +#[derive(FromArgs)] +/// Reach new heights. +pub struct CliConfig { + /// what port the proxy should listen on + #[argh(option, default = "8445")] + pub listen_port: u16, + + /// what port the proxy should listen on + // TODO: use flags for the config path "./data/config/example.toml" + #[argh(option, default = "\"./data/config/example.toml\".to_string()")] + pub rpc_config_path: String, } #[derive(Deserialize)] -pub struct Web3ProxyConfig { - pub listen_port: u16, +pub struct RpcConfig { + // BTreeMap so that iterating keeps the same order + pub balanced_rpc_tiers: BTreeMap>, + pub private_rpcs: HashMap, } #[derive(Deserialize)] @@ -26,7 +34,7 @@ pub struct Web3ConnectionConfig { hard_limit: Option, } -impl RootConfig { +impl RpcConfig { pub async fn try_build(self) -> anyhow::Result { let balanced_rpc_tiers = self .balanced_rpc_tiers diff --git a/web3-proxy/src/main.rs b/web3-proxy/src/main.rs index 9646b0ba..05738bac 100644 --- a/web3-proxy/src/main.rs +++ b/web3-proxy/src/main.rs @@ -13,11 +13,11 @@ use std::sync::Arc; use std::time::Duration; use tokio::sync::RwLock; use tokio::time::sleep; -use tracing::warn; +use tracing::{info, warn}; use warp::Filter; use warp::Reply; -use crate::config::RootConfig; +use crate::config::{CliConfig, RpcConfig}; use crate::connection::JsonRpcRequest; use crate::connections::Web3Connections; @@ -252,19 +252,18 @@ async fn main() -> anyhow::Result<()> { // install global collector configured based on RUST_LOG env var. tracing_subscriber::fmt::init(); - // TODO: use flags for the config path - let config = "./data/config/example.toml"; + let cli_config: CliConfig = argh::from_env(); - let config: String = fs::read_to_string(config)?; - - let config: RootConfig = toml::from_str(&config)?; + info!("Loading rpc config @ {}", cli_config.rpc_config_path); + let rpc_config: String = fs::read_to_string(cli_config.rpc_config_path)?; + let rpc_config: RpcConfig = toml::from_str(&rpc_config)?; // TODO: load the config from yaml instead of hard coding // TODO: support multiple chains in one process? then we could just point "chain.stytt.com" at this and caddy wouldn't need anything else // TODO: be smart about about using archive nodes? have a set that doesn't use archive nodes since queries to them are more valuable - let listen_port = config.config.listen_port; + let listen_port = cli_config.listen_port; - let app = config.try_build().await?; + let app = rpc_config.try_build().await?; let app: Arc = Arc::new(app);