diff --git a/Cargo.toml b/Cargo.toml index c361d56..589284f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitcoin-mock-rpc" -version = "0.0.7" +version = "0.0.8" edition = "2021" [dependencies] @@ -18,3 +18,7 @@ tokio = { version = "1.39.3", features = ["full"]} tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } tower = "0.4.13" +clap = { version = "4.5.16", features = ["derive"] } + +[[bin]] +name = "bitcoin_mock_rpc" diff --git a/src/bin/bitcoin_mock_rpc.rs b/src/bin/bitcoin_mock_rpc.rs new file mode 100644 index 0000000..6d8cd3e --- /dev/null +++ b/src/bin/bitcoin_mock_rpc.rs @@ -0,0 +1,61 @@ +//! # RPC Server Starter +//! +//! This binary can start an RPC server for listening RPC calls. Can be spawned +//! multiple times. Each server will have an independent blockchain. + +use bitcoin_mock_rpc::rpc::spawn_rpc_server; +use clap::Parser; +use std::process::exit; +use tracing::level_filters::LevelFilter; +use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; + +/// Bitcoin Mock Rpc (C) Chainway, 2024 +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// Verbosity level, ranging from 0 (none) to 5 (highest) + #[arg(short, long, default_value_t = 0)] + verbose: u8, + /// Optional host address + #[arg(default_value_t = String::from("127.0.0.1"))] + pub host: String, + /// Optional host port (if not given, requests a random port from OS) + #[arg(default_value_t = 0)] + pub port: u16, +} + +/// Initializes tracing. +fn initialize_logger(level: u8) { + let level = match level { + 0 => return, // No tracing output + 1 => LevelFilter::ERROR, + 2 => LevelFilter::WARN, + 3 => LevelFilter::INFO, + 4 => LevelFilter::DEBUG, + 5 => LevelFilter::TRACE, + _ => { + eprintln!("Verbosity level can only be between 0 and 5 (given {level})!"); + exit(1); + } + }; + + let layer = fmt::layer().with_test_writer(); + let filter = EnvFilter::builder() + .with_default_directive(level.into()) + .from_env_lossy(); + + tracing_subscriber::registry() + .with(layer) + .with(filter) + .init(); +} + +fn main() { + let args = Args::parse(); + initialize_logger(args.verbose); + + let server = spawn_rpc_server(Some(&args.host), Some(args.port)).unwrap(); + println!("Server started at {}", server.0); + + server.1.join().unwrap() +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index e2717a2..0000000 --- a/src/main.rs +++ /dev/null @@ -1,34 +0,0 @@ -//! # RPC Server Starter - -use bitcoin_mock_rpc::rpc::spawn_rpc_server; -use std::env; - -fn main() { - println!("Bitcoin Mock Rpc (C) Chainway, 2024"); - println!( - "Usage: {} [HOST] [PORT]", - env::args().collect::>().first().unwrap() - ); - - let server_info = handle_args(); - let server = spawn_rpc_server(server_info.0.as_deref(), server_info.1).unwrap(); - println!("Server started at {}", server.0); - - server.1.join().unwrap() -} - -fn handle_args() -> (Option, Option) { - let mut ret = (None, None); - - let args: Vec = env::args().collect(); - - if let Some(host) = args.get(1) { - ret.0 = Some(host.to_owned()); - }; - - if let Some(port) = args.get(2) { - ret.1 = Some(port.parse::().unwrap()); - }; - - ret -}