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

@ -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<String, HashMap<String, Web3ConnectionConfig>>,
pub private_rpcs: HashMap<String, Web3ConnectionConfig>,
#[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<String, HashMap<String, Web3ConnectionConfig>>,
pub private_rpcs: HashMap<String, Web3ConnectionConfig>,
}
#[derive(Deserialize)]
@ -26,7 +34,7 @@ pub struct Web3ConnectionConfig {
hard_limit: Option<u32>,
}
impl RootConfig {
impl RpcConfig {
pub async fn try_build(self) -> anyhow::Result<Web3ProxyApp> {
let balanced_rpc_tiers = self
.balanced_rpc_tiers

@ -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<Web3ProxyApp> = Arc::new(app);