Skip to content

Commit

Permalink
Render current project dir in title of status web-ui.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctsrc committed Aug 27, 2024
1 parent e3a11b8 commit 64767e4
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 10 deletions.
131 changes: 131 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ tokio-stream = "0.1.15"
async-stream = "0.3.5"
opener = "0.7.2"
anyhow = "1.0.86"
askama = "0.12.1"
33 changes: 29 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Context};
use askama::Template;
use async_stream::stream;
use bytes::Bytes;
use clap::{crate_version, Parser};
Expand Down Expand Up @@ -26,11 +27,18 @@ use tokio::{fs::File, net::TcpListener};
use tokio_util::io::ReaderStream;
use tracing::{debug, error, info, warn};

#[derive(Template)]
#[template(path = "status-webui/index.htm")]
struct StatusWebUiIndex<'a> {
project_dir: &'a str,
}

static INTERNAL_INDEX_PAGE: OnceLock<Vec<u8>> = OnceLock::new();

static NOT_FOUND_BODY_TEXT: &[u8] = b"HTTP 404. File not found.";
static METHOD_NOT_ALLOWED_BODY_TEXT: &[u8] = b"HTTP 405. Method not allowed.";
static INTERNAL_SERVER_ERROR_BODY_TEXT: &[u8] = b"HTTP 500. Internal server error.";

static INTERNAL_INDEX_PAGE: &[u8] = include_bytes!("../webui-src/html/index.htm");
static INTERNAL_STYLESHEET: &[u8] = include_bytes!("../webui-src/style/main.css");
static INTERNAL_JAVASCRIPT: &[u8] = include_bytes!("../webui-src/js/main.js");

Expand Down Expand Up @@ -121,6 +129,13 @@ async fn main() -> anyhow::Result<()> {
.inspect_err(|e| error!(os_string = ?e, "Fatal: Failed to convert PathBuf to String."))
.map_err(|_| anyhow!("Failed to convert PathBuf to String."))?;

let internal_index_page = StatusWebUiIndex { project_dir: &pdir };
let internal_index_page_rendered = internal_index_page.render()?.as_bytes().to_vec();
INTERNAL_INDEX_PAGE
.set(internal_index_page_rendered)
.inspect_err(|e| error!(existing_value = ?e, "Fatal: OnceLock has existing value."))
.map_err(|_| anyhow!("Failed to set value of OnceLock."))?;

let status_addr = SocketAddr::new(args.status_listen_addr, args.status_listen_port);
let status_tcp = TcpListener::bind(status_addr)
.await
Expand Down Expand Up @@ -452,9 +467,19 @@ async fn request_handler_status(
);

match (method, uri_path) {
(&Method::GET, "") => response_builder
.header(header::CONTENT_TYPE, HeaderValue::from_static(TEXT_HTML))
.body(Either::Left(INTERNAL_INDEX_PAGE.into())),
(&Method::GET, "") => match INTERNAL_INDEX_PAGE.get() {
None => {
error!("Failed to get rendered index page for status web-ui!");
let (status, content_type, body) = server_error();
response_builder
.header(header::CONTENT_TYPE, content_type)
.status(status)
.body(Either::Left(body))
}
Some(internal_index_page) => response_builder
.header(header::CONTENT_TYPE, HeaderValue::from_static(TEXT_HTML))
.body(Either::Left(internal_index_page.as_slice().into())),
},
(&Method::GET, "favicon.ico") => response_builder
.header(header::CONTENT_TYPE, HeaderValue::from_static(IMAGE_X_ICON))
.status(StatusCode::NO_CONTENT)
Expand Down
11 changes: 7 additions & 4 deletions webui-src/html/index.htm → templates/status-webui/index.htm
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<!doctype html>
<html lang=en>
<meta charset=utf-8>
<title>example/out/ – http-horse</title>
<title>Project {{ project_dir }} – http-horse</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel=stylesheet href=/style/main.css>

<div id=outer-main>
<header id=header-main><h1>Status for <code>example/out/</code></h1></header>
<header id=header-main>
<h1>http-horse 🐴</h1>
<h2>Project <code>{{ project_dir }}</code></h2>
</header>

<div id=inner-main>

<section id=pages-and-their-resources>
<header><h2>Pages and their resources</h2></header>
<header><h3>Pages and their resources</h3></header>
<ul id=list-pages-and-their-resources>

<li class=page>
Expand All @@ -35,7 +38,7 @@
</section>

<section id=history-recent-file-system-events>
<header><h2>Recent file system event history</h2></header>
<header><h3>Recent file system event history</h3></header>
<div id=history-entries>
<p>entry</p>
<p>entry</p>
Expand Down
12 changes: 10 additions & 2 deletions webui-src/style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,17 @@ h1, h2, h3, h4, h5, h6 {
}

h1 {
font-size: 1.6rem;
font-size: 1.829rem;
}

h2 {
font-size: 1.4rem;
}

h3 {
font-size: 1.225rem;
}

/*
* ## General list appearance
*/
Expand All @@ -222,10 +226,14 @@ li > ul {
}

#header-main {
padding: 0.618rem 1rem;
padding: 1.618rem 1rem;
overflow-x: hidden;
}

#header-main > h2 {
margin-top: 0.618rem;
}

#inner-main {
padding: 1rem;
}
Expand Down

0 comments on commit 64767e4

Please sign in to comment.