use argh for config

This commit is contained in:
Bryan Stitt 2022-05-05 21:25:40 +00:00
parent 89ced9b152
commit b79a050fd4
2 changed files with 25 additions and 18 deletions

View File

@ -1,3 +1,4 @@
use argh::FromArgs;
use governor::clock::QuantaClock; use governor::clock::QuantaClock;
use serde::Deserialize; use serde::Deserialize;
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
@ -6,17 +7,24 @@ use std::sync::Arc;
use crate::connection::Web3Connection; use crate::connection::Web3Connection;
use crate::Web3ProxyApp; use crate::Web3ProxyApp;
#[derive(Deserialize)] #[derive(FromArgs)]
pub struct RootConfig { /// Reach new heights.
pub config: Web3ProxyConfig, pub struct CliConfig {
// BTreeMap so that iterating keeps the same order /// what port the proxy should listen on
pub balanced_rpc_tiers: BTreeMap<String, HashMap<String, Web3ConnectionConfig>>, #[argh(option, default = "8445")]
pub private_rpcs: HashMap<String, Web3ConnectionConfig>, 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)] #[derive(Deserialize)]
pub struct Web3ProxyConfig { pub struct RpcConfig {
pub listen_port: u16, // BTreeMap so that iterating keeps the same order
pub balanced_rpc_tiers: BTreeMap<String, HashMap<String, Web3ConnectionConfig>>,
pub private_rpcs: HashMap<String, Web3ConnectionConfig>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -26,7 +34,7 @@ pub struct Web3ConnectionConfig {
hard_limit: Option<u32>, hard_limit: Option<u32>,
} }
impl RootConfig { impl RpcConfig {
pub async fn try_build(self) -> anyhow::Result<Web3ProxyApp> { pub async fn try_build(self) -> anyhow::Result<Web3ProxyApp> {
let balanced_rpc_tiers = self let balanced_rpc_tiers = self
.balanced_rpc_tiers .balanced_rpc_tiers

View File

@ -13,11 +13,11 @@ use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tokio::time::sleep; use tokio::time::sleep;
use tracing::warn; use tracing::{info, warn};
use warp::Filter; use warp::Filter;
use warp::Reply; use warp::Reply;
use crate::config::RootConfig; use crate::config::{CliConfig, RpcConfig};
use crate::connection::JsonRpcRequest; use crate::connection::JsonRpcRequest;
use crate::connections::Web3Connections; use crate::connections::Web3Connections;
@ -252,19 +252,18 @@ async fn main() -> anyhow::Result<()> {
// install global collector configured based on RUST_LOG env var. // install global collector configured based on RUST_LOG env var.
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
// TODO: use flags for the config path let cli_config: CliConfig = argh::from_env();
let config = "./data/config/example.toml";
let config: String = fs::read_to_string(config)?; info!("Loading rpc config @ {}", cli_config.rpc_config_path);
let rpc_config: String = fs::read_to_string(cli_config.rpc_config_path)?;
let config: RootConfig = toml::from_str(&config)?; let rpc_config: RpcConfig = toml::from_str(&rpc_config)?;
// TODO: load the config from yaml instead of hard coding // 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: 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 // 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<Web3ProxyApp> = Arc::new(app); let app: Arc<Web3ProxyApp> = Arc::new(app);