Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executable improvements #55

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading