From 80a86e9f8a0f4cec1c5a924b609b0d0daa2e9d75 Mon Sep 17 00:00:00 2001 From: Mark <> Date: Fri, 23 May 2025 10:59:56 +0200 Subject: [PATCH] allow more stuff for "everything else" section, only link to /info/ if a index.html exists --- src/int.html | 4 +- src/main.rs | 118 ++++++++++++++++++++++++++++++-------------------- src/status.rs | 55 ++++++++++++++++++++--- 3 files changed, 120 insertions(+), 57 deletions(-) diff --git a/src/int.html b/src/int.html index afc1de1..fbd2bd6 100644 --- a/src/int.html +++ b/src/int.html @@ -13,10 +13,10 @@

links

In the "running" section on the website, the ids may or may not be links. -If a /srv/tomatenmhark-slashinfo/* entry exists, the link will point to that info site, +If a /srv/tomatenmhark-slashinfo/*/index.html entry exists, the link will point to that info site, If only a /srv/tomatenmhark-redirect/* entry exists, the link will follow that redirect, and if neither exist, there will be no link.
-In the "everything else" section, every entry links to its info page. +In the "everything else" section, every entry links to its info page, if there is one.

/tmp/tomatenmhark-status-*

For each file /tmp/tomatenmhark-status-*, an entry will be created on tomatenmhark.org. diff --git a/src/main.rs b/src/main.rs index 753d28b..856ba0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,64 +64,86 @@ span { o.push_str("

:) tomatenmhark :)

"); let status = data.status_async(dbg).await; let time_queried = Instant::now(); + fn push_elem_to_html( + id: &str, + info: bool, + redirect: bool, + status: &str, + additional: Option<&str>, + dur: Option, + o: &mut String, + dbg: bool, + ) { + o.push_str("
  • "); + if info { + o.push_str(r#""#); + } else if redirect { + o.push_str(r#""#); + } else { + o.push_str(""); + } + o.push_str(&html_escape::encode_text(&id)); + if info || redirect { + o.push_str(""); + } else { + o.push_str(""); + } + o.push_str(": "); + if additional.is_some() { + o.push_str(r#""#); + } + o.push_str(&html_escape::encode_text(&status)); + if let Some(additional) = additional { + o.push_str(r#"
    "#); + o.push_str( + &html_escape::encode_text(additional) + .replace('\r', "") + .replace('\n', "
    "), + ); + o.push_str("
    "); + } + if dbg { + if let Some(dur) = dur { + o.push_str(&format!(" | {}ms", dur.as_millis())); + } + } + o.push_str("
  • "); + } if !status.0.is_empty() { o.push_str("

    things running on the server

    "); o.push_str(""); } if !status.1.is_empty() { o.push_str("

    everything else

    "); o.push_str(""); } diff --git a/src/status.rs b/src/status.rs index f6bef59..d709143 100644 --- a/src/status.rs +++ b/src/status.rs @@ -7,7 +7,7 @@ const REDIRECT: &'static str = "/srv/tomatenmhark-redirect/"; pub struct Status( pub BTreeMap, Option)>, - pub BTreeMap, + pub BTreeMap)>, pub Duration, ); impl Status { @@ -46,7 +46,29 @@ impl Status { let mut p = f.path(); p.push("desc"); if let Ok(desc) = std::fs::read_to_string(&p) { - rest.insert(id.to_owned(), desc); + let info = Path::new(SLASHINFO).join(id).join("index.html"); + let info = info.starts_with(SLASHINFO) + && info.try_exists().ok() == Some(true); + let redirect = Path::new(REDIRECT).join(id); + let redirect = redirect.starts_with(REDIRECT) + && redirect.try_exists().ok() == Some(true); + let desc = desc.trim(); + if let Some(i) = desc.find('\n') { + rest.insert( + id.to_owned(), + ( + info, + redirect, + desc[0..i].trim().to_owned(), + Some(desc[i + 1..].trim().to_owned()), + ), + ); + } else { + rest.insert( + id.to_owned(), + (info, redirect, desc.to_owned(), None), + ); + } } } } @@ -90,7 +112,26 @@ impl Status { let mut p = f.path(); p.push("desc"); if let Ok(desc) = tokio::fs::read_to_string(&p).await { - rest.insert(id.to_owned(), desc); + let info = Path::new(SLASHINFO).join(id).join("index.html"); + let info = info.starts_with(SLASHINFO) + && tokio::fs::try_exists(info).await.ok() == Some(true); + let redirect = Path::new(REDIRECT).join(id); + let redirect = redirect.starts_with(REDIRECT) + && tokio::fs::try_exists(redirect).await.ok() == Some(true); + let desc = desc.trim(); + if let Some(i) = desc.find('\n') { + rest.insert( + id.to_owned(), + ( + info, + redirect, + desc[0..i].trim().to_owned(), + Some(desc[i + 1..].trim().to_owned()), + ), + ); + } else { + rest.insert(id.to_owned(), (info, redirect, desc.to_owned(), None)); + } } } } @@ -113,7 +154,7 @@ fn query_status_sync(mut func: impl FnMut(&str, bool, bool, &str, Option