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

Display chain in header #809

Merged
merged 1 commit into from
Nov 16, 2022
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
36 changes: 26 additions & 10 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl Server {
}

async fn ordinal(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(ordinal)): Path<DeserializeFromStr<Ordinal>>,
) -> ServerResult<PageHtml> {
Expand All @@ -313,14 +314,14 @@ impl Server {
))
})?,
}
.page(),
.page(chain),
)
}

async fn output(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(outpoint): Path<OutPoint>,
Extension(chain): Extension<Chain>,
) -> ServerResult<PageHtml> {
let list = index
.list(outpoint)
Expand All @@ -343,11 +344,12 @@ impl Server {
chain,
output,
}
.page(),
.page(chain),
)
}

async fn range(
Extension(chain): Extension<Chain>,
Path((DeserializeFromStr(start), DeserializeFromStr(end))): Path<(
DeserializeFromStr<Ordinal>,
DeserializeFromStr<Ordinal>,
Expand All @@ -358,7 +360,7 @@ impl Server {
Ordering::Greater => Err(ServerError::BadRequest(
"range start greater than range end".to_string(),
)),
Ordering::Less => Ok(RangeHtml { start, end }.page()),
Ordering::Less => Ok(RangeHtml { start, end }.page(chain)),
}
}

Expand All @@ -368,20 +370,24 @@ impl Server {
})?))
}

async fn home(Extension(index): Extension<Arc<Index>>) -> ServerResult<PageHtml> {
async fn home(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml> {
Ok(
HomeHtml::new(
index
.blocks(100)
.map_err(|err| ServerError::Internal(anyhow!("error getting blocks: {err}")))?,
)
.page(),
.page(chain),
)
}

async fn block(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(query)): Path<DeserializeFromStr<BlockQuery>>,
index: Extension<Arc<Index>>,
) -> ServerResult<PageHtml> {
let (block, height) = match query {
BlockQuery::Height(height) => {
Expand Down Expand Up @@ -427,7 +433,7 @@ impl Server {
.height()
.map_err(|err| ServerError::Internal(anyhow!("failed to get index height: {err}")))?,
)
.page(),
.page(chain),
)
}

Expand All @@ -448,7 +454,7 @@ impl Server {
.ok_or_else(|| ServerError::NotFound(format!("transaction {txid} unknown")))?,
chain,
)
.page(),
.page(chain),
)
}

Expand Down Expand Up @@ -546,6 +552,7 @@ impl Server {
}

async fn input(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(path): Path<(u64, usize, usize)>,
) -> Result<PageHtml, ServerError> {
Expand All @@ -565,7 +572,7 @@ impl Server {
.nth(path.2)
.ok_or_else(not_found)?;

Ok(InputHtml { path, input }.page())
Ok(InputHtml { path, input }.page(chain))
}

async fn faq() -> Redirect {
Expand Down Expand Up @@ -1101,6 +1108,15 @@ mod tests {
);
}

#[test]
fn nav_displays_chain() {
TestServer::new().assert_response_regex(
"/",
StatusCode::OK,
".*<a href=/>Ordinals<sup>regtest</sup></a>.*",
);
}

#[test]
fn home_block_limit() {
let test_server = TestServer::new();
Expand Down
60 changes: 55 additions & 5 deletions src/subcommand/server/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ mod transaction;
#[derive(Boilerplate)]
pub(crate) struct PageHtml {
content: Box<dyn Content>,
chain: Chain,
}

impl PageHtml {
pub(crate) fn new<T: Content + 'static>(content: T) -> Self {
pub(crate) fn new<T: Content + 'static>(content: T, chain: Chain) -> Self {
Self {
content: Box::new(content),
chain,
}
}
}

pub(crate) trait Content: Display + 'static {
fn title(&self) -> String;

fn page(self) -> PageHtml
fn page(self, chain: Chain) -> PageHtml
where
Self: Sized,
{
PageHtml::new(self)
PageHtml::new(self, chain)
}
}

Expand All @@ -48,7 +50,7 @@ mod tests {
use super::*;

#[test]
fn page() {
fn page_mainnet() {
struct Foo;

impl Display for Foo {
Expand All @@ -64,7 +66,7 @@ mod tests {
}

assert_regex_match!(
Foo.page().to_string(),
Foo.page(Chain::Mainnet).to_string(),
"<!doctype html>
<html lang=en>
<head>
Expand All @@ -91,6 +93,54 @@ mod tests {
</main>
</body>
</html>
"
);
}

#[test]
fn page_signet() {
struct Foo;

impl Display for Foo {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "<h1>Foo</h1>")
}
}

impl Content for Foo {
fn title(&self) -> String {
"Foo".to_string()
}
}

assert_regex_match!(
Foo.page(Chain::Signet).to_string(),
"<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<meta name=format-detection content='telephone=no'>
<meta name=viewport content='width=device-width,initial-scale=1.0'>
<title>Foo</title>
<link href=/static/index.css rel=stylesheet>
<link href=/static/modern-normalize.css rel=stylesheet>
</head>
<body>
<header>
<nav>
<a href=/>Ordinals<sup>signet</sup></a>
.*
<form action=/search method=get>
<input type=text .*>
<input type=submit value=Search>
</form>
</nav>
</header>
<main>
<h1>Foo</h1>
</main>
</body>
</html>
"
);
}
Expand Down
2 changes: 1 addition & 1 deletion templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<body>
<header>
<nav>
<a href=/>Ordinals</a>
<a href=/>Ordinals{% if self.chain != Chain::Mainnet { %}<sup>{{self.chain}}</sup>{% } %}</a>
<a href=https://docs.ordinals.com/>Docs</a>
<a href=https://github.com/casey/ord>GitHub</a>
<a href=/clock>Clock</a>
Expand Down