|
| 1 | +//! # RPC Server Starter |
| 2 | +//! |
| 3 | +//! This binary can start an RPC server for listening RPC calls. Can be spawned |
| 4 | +//! multiple times. Each server will have an independent blockchain. |
| 5 | +
|
| 6 | +use std::process::exit; |
| 7 | +use bitcoin_mock_rpc::rpc::spawn_rpc_server; |
| 8 | +use clap::Parser; |
| 9 | +use tracing::level_filters::LevelFilter; |
| 10 | +use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; |
| 11 | + |
| 12 | +/// Bitcoin Mock Rpc (C) Chainway, 2024 |
| 13 | +#[derive(Parser, Debug)] |
| 14 | +#[command(version, about, long_about = None)] |
| 15 | +struct Args { |
| 16 | + /// Verbosity level, ranging from 0 (none) to 5 (highest) |
| 17 | + #[arg(short, long, default_value_t = 0)] |
| 18 | + verbose: u8, |
| 19 | + /// Optional host address |
| 20 | + #[arg(default_value_t = String::from("127.0.0.1"))] |
| 21 | + pub host: String, |
| 22 | + /// Optional host port (if not given, requests a random port from OS) |
| 23 | + #[arg(default_value_t = 0)] |
| 24 | + pub port: u16, |
| 25 | +} |
| 26 | + |
| 27 | +/// Initializes tracing. |
| 28 | +fn initialize_logger(level: u8) { |
| 29 | + let level = match level { |
| 30 | + 0 => return, |
| 31 | + 1 => LevelFilter::ERROR, |
| 32 | + 2 => LevelFilter::WARN, |
| 33 | + 3 => LevelFilter::INFO, |
| 34 | + 4 => LevelFilter::DEBUG, |
| 35 | + 5 => LevelFilter::TRACE, |
| 36 | + _ => { |
| 37 | + eprintln!("Verbosity level can be between 0 and 5 (given {level})!"); |
| 38 | + exit(1); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + let layer = fmt::layer().with_test_writer(); |
| 43 | + let filter = EnvFilter::builder() |
| 44 | + .with_default_directive(level.into()).from_env_lossy(); |
| 45 | + |
| 46 | + tracing_subscriber::registry() |
| 47 | + .with(layer) |
| 48 | + .with(filter) |
| 49 | + .init(); |
| 50 | +} |
| 51 | + |
| 52 | +fn main() { |
| 53 | + let args = Args::parse(); |
| 54 | + initialize_logger(args.verbose); |
| 55 | + |
| 56 | + let server = spawn_rpc_server(Some(&args.host), Some(args.port)).unwrap(); |
| 57 | + println!("Server started at {}", server.0); |
| 58 | + |
| 59 | + server.1.join().unwrap() |
| 60 | +} |
0 commit comments