Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
Add runestone server
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Sep 27, 2023
1 parent a0e8158 commit 57d4ca6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ license = "CC0-1.0"
autotests = false

[dependencies]
anyhow = "1.0.75"
axum = "0.6.1"
axum-server = "0.5.0"
bitcoin = "0.30.1"
clap = { version = "4.4.5", features = ["derive"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
tokio = { version = "1.17.0", features = ["rt-multi-thread"] }

[dev-dependencies]
executable-path = "1.0.0"
reqwest = { version = "0.11.20", features = ["blocking"] }

[[test]]
name = "integration"
Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use {
axum::routing::get,
axum::Router,
bitcoin::{consensus::Decodable, Transaction},
clap::{
builder::{
Expand All @@ -8,7 +10,8 @@ use {
Parser,
},
runestone::Runestone,
std::{error::Error, io},
std::{error::Error, io, net::ToSocketAddrs},
tokio::runtime::Runtime,
};

#[derive(Parser)]
Expand All @@ -25,6 +28,8 @@ enum Subcommand {
about = "Read a bitcoin transaction from standard input and print a JSON representation of its runestone."
)]
Decipher,
#[command(about = "Start the explorer.")]
Server,
}

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -38,7 +43,18 @@ fn main() -> Result<(), Box<dyn Error>> {
serde_json::to_writer_pretty(&io::stdout(), &message)?;
println!();
}
Subcommand::Server => Runtime::new()?.block_on(async {
let addr = ("0.0.0.0", 80).to_socket_addrs()?.next().unwrap();

axum_server::Server::bind(addr)
.serve(Router::new().route("/", get(home)).into_make_service())
.await
})?,
}

Ok(())
}

async fn home() -> &'static str {
"Hello, world!"
}
5 changes: 4 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use {
bitcoin::{consensus::Encodable, locktime, opcodes, script, Transaction, TxOut},
executable_path::executable_path,
reqwest::blocking as reqwest,
std::{
io::Write,
process::{Command, Stdio},
str,
str, thread,
time::Duration,
},
};

mod decipher;
mod server;
40 changes: 40 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::*;

struct KillOnDrop(std::process::Child);

impl Drop for KillOnDrop {
fn drop(&mut self) {
assert!(Command::new("kill")
.arg(self.0.id().to_string())
.status()
.unwrap()
.success());
}
}

#[test]
fn server_returns_homepage() {
let _server = KillOnDrop(
Command::new(executable_path("runestone"))
.arg("server")
.spawn()
.unwrap(),
);

for i in 0..100 {
if reqwest::get("http://localhost").is_ok() {
break;
}

if i == 99 {
panic!("server failed to start");
}

thread::sleep(Duration::from_millis(100));
}

assert_eq!(
reqwest::get("http://localhost").unwrap().text().unwrap(),
"Hello, world!"
);
}

0 comments on commit 57d4ca6

Please sign in to comment.