Skip to content

Commit

Permalink
http: Implement GET /banner.txt (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Oct 1, 2020
1 parent 32ef855 commit 104df4f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

- HTTP API: Fix `GET /block/tip` (#46)

- HTTP API: Add `GET /banner.txt` (#44)

- Tests: Upgrade to Electrum v4

## 0.1.4 - 2020-06-22
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,11 @@ Dumps the contents of the index store as JSON.

Dumps the contents of the index store as a debug string.

#### `GET /banner.txt`

Get the welcome banner text.
([example](https://gist.githubusercontent.com/shesek/4986c4291df1a7c6de62c20bc72e58bf/raw/42539cd10f1836ae511f4c2ec7b4fc82ad52252a/bwt-welcome-banner.txt))

## Web Hooks

> If you're building bwt from source, you'll need to set `--features webhooks` to enable web hooks support. This will also require to `apt install libssl-dev pkg-config`.
Expand Down
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl App {
wait_bitcoind(&rpc)?;

if config.startup_banner {
println!("{}", banner::get_welcome_banner(&query)?);
println!("{}", banner::get_welcome_banner(&query, false)?);
}

// do an initial sync without keeping track of updates
Expand Down
12 changes: 8 additions & 4 deletions src/banner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const TARGET_BLOCK_SPACING: u64 = constants::TARGET_BLOCK_SPACING as u64;
const INITIAL_REWARD: u64 = 50 * constants::COIN_VALUE;
const HALVING_INTERVAL: u64 = 210_000;

pub fn get_welcome_banner(query: &Query) -> Result<String> {
pub fn get_welcome_banner(query: &Query, omit_donation: bool) -> Result<String> {
let rpc = query.rpc();

let net_info = rpc.get_network_info_()?;
Expand Down Expand Up @@ -116,8 +116,8 @@ pub fn get_welcome_banner(query: &Query) -> Result<String> {
MEMPOOL: 💭 {mempool_size} / {mempool_n_tx} / ᴍɪɴ {mempool_min_fee} sᴀᴛ/ᴠʙ
FEES EST: 🏷️ 20 ᴍɪɴᴜᴛᴇs: {est_20m} / 3 ʜᴏᴜʀs: {est_3h} / 1 ᴅᴀʏ: {est_1d} (sᴀᴛ/ᴠʙ)
SUPPORT DEV: 🚀 bc1qmuagsjvq0lh3admnafk0qnlql0vvxv08au9l2d / https://btcpay.shesek.info
"#,
{donation_frag}"#,
modes = modes.join(" "),
client_name = to_widetext(&net_info.subversion),
chain_name = to_smallcaps(&chain_name),
connected_peers = net_info.connections,
Expand All @@ -144,10 +144,14 @@ pub fn get_welcome_banner(query: &Query) -> Result<String> {
est_20m = est_20m,
est_3h = est_3h,
est_1d = est_1d,
modes = modes.join(" "),
ver_line1 = ver_lines.0,
ver_line2 = ver_lines.1,
ver_line3 = ver_lines.2,
donation_frag = if !omit_donation {
" SUPPORT DEV: 🚀 bc1qmuagsjvq0lh3admnafk0qnlql0vvxv08au9l2d / https://btcpay.shesek.info\n"
} else {
""
},
))
}

Expand Down
2 changes: 1 addition & 1 deletion src/electrum/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Connection {
}

fn server_banner(&self) -> Result<Value> {
Ok(json!(get_welcome_banner(&self.query)?))
Ok(json!(get_welcome_banner(&self.query, false)?))
}

fn server_donation_address(&self) -> Result<Value> {
Expand Down
10 changes: 9 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bitcoin_hashes::hex::FromHex;

use crate::error::{fmt_error_chain, BwtError, Error, OptionExt};
use crate::types::{BlockId, ScriptHash};
use crate::{store, IndexChange, Query};
use crate::{banner, store, IndexChange, Query};

type SyncChanSender = Arc<Mutex<mpsc::Sender<()>>>;

Expand Down Expand Up @@ -414,6 +414,13 @@ async fn run(
.and(query.clone())
.map(|query: Arc<Query>| query.debug_index());

// GET /banner.txt
let banner_handler = warp::get()
.and(warp::path!("banner.txt"))
.and(query.clone())
.map(|query: Arc<Query>| banner::get_welcome_banner(&query, true))
.map(handle_error);

// POST /sync
let sync_handler = warp::post()
.and(warp::path!("sync"))
Expand Down Expand Up @@ -455,6 +462,7 @@ async fn run(
fee_estimate_handler,
dump_handler,
debug_handler,
banner_handler,
sync_handler,
warp::any().map(|| StatusCode::NOT_FOUND)
)
Expand Down

0 comments on commit 104df4f

Please sign in to comment.