From fe41a083c9d3ef6342f7cb2f3647829002258b77 Mon Sep 17 00:00:00 2001 From: mark <> Date: Mon, 10 Mar 2025 15:32:22 +0100 Subject: [PATCH] customizable index.html --- message.html | 1 + src/index.html | 8 ++------ src/main.rs | 45 +++++++++++++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/message.html b/message.html index f4f66d1..f9051ab 100644 --- a/message.html +++ b/message.html @@ -1,3 +1,4 @@ +


Streiks und Ausfälle

diff --git a/src/index.html b/src/index.html index e1ba5fb..ce1a8e2 100644 --- a/src/index.html +++ b/src/index.html @@ -71,8 +71,8 @@ async function con(change) { } } +

Zum Bahnhof Merklingen (Ankunft 8:40 | Heute wenn es noch nicht 8:30 ist, sonst wird Bus von morgen geprüft) von
-
/ / / @@ -86,15 +86,11 @@ async function con(change) { / / -
+






-%%MESSAGE%% -

-
-

Diese Seite soll eine einfache Möglichkeit darstellen, zu prüfen, ob der Bus und Zug von/nach Merklingen heute kommt, oder ob er mal wieder streikt oder aus sonstigen Gründen fehlt.
diff --git a/src/main.rs b/src/main.rs index 0f5c1b1..d093dc1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,11 @@ async fn rocket() -> _ { rocket::build() .manage(Arc::new(())) .manage(Mutex::new( - Option::<(Instant, Result)>::None, + Option::<( + Instant, + Result, + String, + )>::None, )) .mount( "/", @@ -33,16 +37,30 @@ async fn rocket() -> _ { #[get("/")] async fn index( - departures: &State)>>>, + departures: &State< + Mutex< + Option<( + Instant, + Result, + String, + )>, + >, + >, ) -> RawHtml { let mut departures = departures.lock().await; - if departures.as_ref().is_none_or(|(last_updated, result)| { + if departures.as_ref().is_none_or(|(last_updated, result, _)| { last_updated.elapsed() > Duration::from_secs(30 * if result.is_ok() { 5 } else { 1 }) }) { - *departures = Some((Instant::now(), bahnhof::departures_merklingen().await)) + *departures = Some(( + Instant::now(), + bahnhof::departures_merklingen().await, + tokio::fs::read_to_string("index.html") + .await + .unwrap_or_else(|_| include_str!("index.html").to_owned()), + )); } let departures_str = match &*departures { - Some((_, Ok(departures))) => { + Some((_, Ok(departures), _)) => { let mut departures_str = String::new(); departures_str.push_str(r#""#); let mut table_rows = BTreeMap::<_, (String, String, usize, usize)>::new(); @@ -220,20 +238,15 @@ async fn index( departures_str.push_str("
"); departures_str } - Some((_, Err(e))) => format!("{}", html_escape::encode_safe(e)), + Some((_, Err(e), _)) => format!("{}", html_escape::encode_safe(e)), None => "failed".to_owned(), }; RawHtml( - include_str!("index.html") - .replace("%%DEPARTURES%%", &departures_str) - .replace( - "%%MESSAGE%%", - tokio::fs::read_to_string("message.html") - .await - .as_ref() - .map(|v| v.as_str()) - .unwrap_or(""), - ), + departures + .as_ref() + .map(|(_, _, index)| index.as_str()) + .unwrap_or(include_str!("index.html")) + .replace("%%DEPARTURES%%", &departures_str), ) }