Skip to content

Commit

Permalink
Add search box to homepage (ordinals#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Sep 5, 2022
1 parent 7adb39e commit 36956ab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use {
range::RangeHtml, transaction::TransactionHtml, Content,
},
},
axum::{body, http::header, response::Response},
axum::{
body,
http::header,
response::{Redirect, Response},
},
rust_embed::RustEmbed,
rustls_acme::{
acme::{LETS_ENCRYPT_PRODUCTION_DIRECTORY, LETS_ENCRYPT_STAGING_DIRECTORY},
Expand All @@ -24,6 +28,11 @@ use {
mod deserialize_ordinal_from_str;
mod templates;

#[derive(Deserialize)]
struct Search {
query: String,
}

#[derive(RustEmbed)]
#[folder = "static"]
struct StaticAssets;
Expand Down Expand Up @@ -104,6 +113,7 @@ impl Server {
.route("/ordinal/:ordinal", get(Self::ordinal))
.route("/output/:output", get(Self::output))
.route("/range/:start/:end", get(Self::range))
.route("/search", get(Self::search))
.route("/static/*path", get(Self::static_asset))
.route("/status", get(Self::status))
.route("/tx/:txid", get(Self::transaction))
Expand Down Expand Up @@ -411,6 +421,10 @@ impl Server {
)
}

async fn search(search: extract::Query<Search>) -> Redirect {
Redirect::to(&format!("/ordinal/{}", search.query))
}

async fn favicon() -> impl IntoResponse {
Self::static_asset(extract::Path("/favicon.png".to_string())).await
}
Expand Down
5 changes: 5 additions & 0 deletions src/subcommand/server/templates/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ mod tests {
.to_string(),
"<h1>Ordinals</h1>
<nav>.*</nav>
<h2>Search</h2>
<form action=/search method=get>
<input type=text name=query>
<input type=submit>
</form>
<h2>Recent Blocks</h2>
<ol start=1 reversed class=monospace>
<li><a href=/block/1{64} class=uncommon>1{64}</a></li>
Expand Down
5 changes: 5 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ <h1>Ordinals</h1>
<a href=https://github.com/casey/ord>GitHub</a>
<a href=https://discord.gg/87cjuz4FYg>Discord</a>
</nav>
<h2>Search</h2>
<form action=/search method=get>
<input type=text name=query>
<input type=submit>
</form>
<h2>Recent Blocks</h2>
<ol start={{self.last}} reversed class=monospace>
%% for (rarity, hash) in &self.blocks {
Expand Down
6 changes: 6 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn home() {
200,
".*<title>Ordinals</title>.*<h1>Ordinals</h1>
<nav>.*</nav>
.*
<h2>Recent Blocks</h2>
<ol start=1 reversed class=monospace>
<li><a href=/block/[[:xdigit:]]{64} class=uncommon>[[:xdigit:]]{64}</a></li>
Expand Down Expand Up @@ -369,3 +370,8 @@ fn clock_is_served_with_svg_extension() {
let mut state = State::new();
state.request_regex("clock.svg", 200, "<svg.*");
}

#[test]
fn search_returns_ordinal() {
State::new().request_regex("search?query=0", 200, ".*<h1>Ordinal 0</h1>.*");
}

0 comments on commit 36956ab

Please sign in to comment.