customizable index.html

This commit is contained in:
mark 2025-03-10 15:32:22 +01:00
parent 7435065d18
commit fe41a083c9
3 changed files with 32 additions and 22 deletions

View File

@ -1,3 +1,4 @@
<br><br><br>
<h2>Streiks und Ausfälle</h2>
<!-- insert p element ^here^ for each known thing -->
<p>

View File

@ -71,8 +71,8 @@ async function con(change) {
}
}
</script>
<p>
<div>Zum Bahnhof Merklingen <small>(Ankunft 8:40 <small> | Heute wenn es noch nicht 8:30 ist, sonst wird Bus von morgen geprüft</small>)</small> von</div>
<div>
<button id="widderstall" onclick="con('967Widderstall')">Widderstall</button> /
<button id="hohenstadt_kirche" onclick="con('967HohenstadtKirche')">Hohenstadt<small> Kirche</small></button> /
<button id="hohenstadt_waltertal" onclick="con('967HohenstadtWaltertal')">Hohenstadt<small> Abzw. Waltertal</small></button> /
@ -86,15 +86,11 @@ async function con(change) {
<button id="wiesensteig_schöntalweg" onclick="con('967WiesensteigSchontalweg')">Wiesensteig<small> Schöntalweg</small></button> /
<button id="wiesensteig_brunnengarten" onclick="con('967WiesensteigBrunnengarten')">Wiesensteig<small> Brunnengarten</small></button> /
<button id="wiesensteig_rathaus" onclick="con('967WiesensteigRathaus')">Wiesensteig<small> Rathaus</small></button>
</div>
</p>
<script>con();</script>
<br><br>
<hr>
<br><br>
%%MESSAGE%%
<br><br>
<hr>
<br><br>
<p>
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.<br>

View File

@ -15,7 +15,11 @@ async fn rocket() -> _ {
rocket::build()
.manage(Arc::new(()))
.manage(Mutex::new(
Option::<(Instant, Result<bahnhof::DeparturesMerklingen, String>)>::None,
Option::<(
Instant,
Result<bahnhof::DeparturesMerklingen, String>,
String,
)>::None,
))
.mount(
"/",
@ -33,16 +37,30 @@ async fn rocket() -> _ {
#[get("/")]
async fn index(
departures: &State<Mutex<Option<(Instant, Result<bahnhof::DeparturesMerklingen, String>)>>>,
departures: &State<
Mutex<
Option<(
Instant,
Result<bahnhof::DeparturesMerklingen, String>,
String,
)>,
>,
>,
) -> RawHtml<String> {
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#"<table style="width:100%;">"#);
let mut table_rows = BTreeMap::<_, (String, String, usize, usize)>::new();
@ -220,20 +238,15 @@ async fn index(
departures_str.push_str("</table>");
departures_str
}
Some((_, Err(e))) => format!("<small>{}</small>", html_escape::encode_safe(e)),
Some((_, Err(e), _)) => format!("<small>{}</small>", html_escape::encode_safe(e)),
None => "<small>failed</small>".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),
)
}