allow more stuff for "everything else" section, only link to /info/ if a index.html exists

This commit is contained in:
Mark 2025-05-23 10:59:56 +02:00
parent 92141190aa
commit 80a86e9f8a
3 changed files with 120 additions and 57 deletions

View File

@ -13,10 +13,10 @@
<h4>links</h4>
In the "running" section on the website, the ids may or may not be links.
If a <code>/srv/tomatenmhark-slashinfo/*</code> entry exists, the link will point to that info site,
If a <code>/srv/tomatenmhark-slashinfo/*/index.html</code> entry exists, the link will point to that info site,
If only a <code>/srv/tomatenmhark-redirect/*</code> entry exists, the link will follow that redirect,
and if neither exist, there will be no link.<br>
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.
<h4>/tmp/tomatenmhark-status-*</h4>
For each file <code>/tmp/tomatenmhark-status-*</code>, an entry will be created on <a href="/">tomatenmhark.org</a>.

View File

@ -64,64 +64,86 @@ span {
o.push_str("<h2>:) tomatenmhark :)</h2>");
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<Duration>,
o: &mut String,
dbg: bool,
) {
o.push_str("<li>");
if info {
o.push_str(r#"<a href="/info/"#);
o.push_str(&html_escape::encode_double_quoted_attribute(&id));
o.push_str(r#"">"#);
} else if redirect {
o.push_str(r#"<a href="/"#);
o.push_str(&html_escape::encode_double_quoted_attribute(&id));
o.push_str(r#"">"#);
} else {
o.push_str("<span>");
}
o.push_str(&html_escape::encode_text(&id));
if info || redirect {
o.push_str("</a>");
} else {
o.push_str("</span>");
}
o.push_str(": ");
if additional.is_some() {
o.push_str(r#"<em class="oht">"#);
}
o.push_str(&html_escape::encode_text(&status));
if let Some(additional) = additional {
o.push_str(r#"</em><div class="ohs">"#);
o.push_str(
&html_escape::encode_text(additional)
.replace('\r', "")
.replace('\n', "<br>"),
);
o.push_str("</div>");
}
if dbg {
if let Some(dur) = dur {
o.push_str(&format!(" | {}ms", dur.as_millis()));
}
}
o.push_str("</li>");
}
if !status.0.is_empty() {
o.push_str("<h3>things running on the server</h3>");
o.push_str("<ul>");
for (id, (info, redirect, status, additional, dur)) in status.0.iter() {
o.push_str("<li>");
if *info {
o.push_str(r#"<a href="/info/"#);
o.push_str(&html_escape::encode_double_quoted_attribute(&id));
o.push_str(r#"">"#);
} else if *redirect {
o.push_str(r#"<a href="/"#);
o.push_str(&html_escape::encode_double_quoted_attribute(&id));
o.push_str(r#"">"#);
} else {
o.push_str("<span>");
}
o.push_str(&html_escape::encode_text(&id));
if *info || *redirect {
o.push_str("</a>");
} else {
o.push_str("</span>");
}
o.push_str(": ");
if additional.is_some() {
o.push_str(r#"<em class="oht">"#);
}
o.push_str(&html_escape::encode_text(&status));
if let Some(additional) = additional {
o.push_str(r#"</em><div class="ohs">"#);
o.push_str(
&html_escape::encode_text(additional)
.replace('\r', "")
.replace('\n', "<br>"),
);
o.push_str("</div>");
}
if dbg {
if let Some(dur) = *dur {
o.push_str(&format!(" | {}ms", dur.as_millis()));
}
}
o.push_str("</li>");
push_elem_to_html(
id,
*info,
*redirect,
status,
additional.as_ref().map(|v| v.as_str()),
*dur,
&mut o,
dbg,
);
}
o.push_str("</ul>");
}
if !status.1.is_empty() {
o.push_str("<h3>everything else</h3>");
o.push_str("<ul>");
for (id, text) in status.1.iter() {
o.push_str("<li>");
o.push_str(r#"<a href="/info/"#);
o.push_str(&html_escape::encode_double_quoted_attribute(&id));
o.push_str(r#"">"#);
o.push_str(&html_escape::encode_text(&id));
o.push_str("</a>");
o.push_str(": ");
o.push_str(&html_escape::encode_text(&text));
o.push_str("</li>");
for (id, (info, redirect, status, additional)) in status.1.iter() {
push_elem_to_html(
id,
*info,
*redirect,
status,
additional.as_ref().map(|v| v.as_str()),
None,
&mut o,
dbg,
);
}
o.push_str("</ul>");
}

View File

@ -7,7 +7,7 @@ const REDIRECT: &'static str = "/srv/tomatenmhark-redirect/";
pub struct Status(
pub BTreeMap<String, (bool, bool, String, Option<String>, Option<Duration>)>,
pub BTreeMap<String, String>,
pub BTreeMap<String, (bool, bool, String, Option<String>)>,
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<Duratio
.map(|v| v.trim_end())
{
if !status.is_empty() {
let info = Path::new(SLASHINFO).join(id);
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);
@ -143,7 +184,7 @@ fn query_status_sync(mut func: impl FnMut(&str, bool, bool, &str, Option<Duratio
let out = String::from_utf8_lossy(&output.stdout);
let out = out.trim_end();
if dbg || !out.is_empty() {
let info = Path::new(SLASHINFO).join(id);
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);
@ -175,7 +216,7 @@ async fn query_status_async(
.map(|v| v.trim_end())
{
if !status.is_empty() {
let info = Path::new(SLASHINFO).join(id);
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);
@ -204,7 +245,7 @@ async fn query_status_async(
let out = String::from_utf8_lossy(&output.stdout);
let out = out.trim_end();
if dbg || !out.is_empty() {
let info = Path::new(SLASHINFO).join(id);
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);