Skip to content

Commit

Permalink
Merge pull request #55 from chainwayxyz/main_executable_improvements
Browse files Browse the repository at this point in the history
Executable improvements
  • Loading branch information
ceyhunsen authored Aug 28, 2024
2 parents de66137 + 16f9dfd commit d711572
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 35 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitcoin-mock-rpc"
version = "0.0.7"
version = "0.0.8"
edition = "2021"

[dependencies]
Expand All @@ -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"
61 changes: 61 additions & 0 deletions src/bin/bitcoin_mock_rpc.rs
Original file line number Diff line number Diff line change
@@ -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()
}
34 changes: 0 additions & 34 deletions src/main.rs

This file was deleted.

0 comments on commit d711572

Please sign in to comment.