Skip to content

Commit 4a84a95

Browse files
committed
bin: Move main.rs to bin/.
1 parent de66137 commit 4a84a95

File tree

3 files changed

+65
-35
lines changed

3 files changed

+65
-35
lines changed

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bitcoin-mock-rpc"
3-
version = "0.0.7"
3+
version = "0.0.8"
44
edition = "2021"
55

66
[dependencies]
@@ -18,3 +18,7 @@ tokio = { version = "1.39.3", features = ["full"]}
1818
tracing = "0.1.40"
1919
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
2020
tower = "0.4.13"
21+
clap = { version = "4.5.16", features = ["derive"] }
22+
23+
[[bin]]
24+
name = "bitcoin_mock_rpc"

src/bin/bitcoin_mock_rpc.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

src/main.rs

-34
This file was deleted.

0 commit comments

Comments
 (0)