generated from Austionian/axum_tailwind_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use maud, remove context from being needed in threads
- Loading branch information
1 parent
9c1fa19
commit 381cdfa
Showing
13 changed files
with
176 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
tailwindcss | ||
mutants* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,36 @@ use axum::{ | |
http::StatusCode, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use maud::{html, Markup, PreEscaped}; | ||
use std::{convert::Infallible, sync::Arc}; | ||
use tokio::sync::{mpsc, Mutex}; | ||
use tokio::sync::mpsc; | ||
|
||
/// This is ugly. Might be worth reverting to tera to create the | ||
/// inline JS. Allows not having to pass the context around in a | ||
/// mutex to the other threads though. | ||
fn error_markup(error_type: &str, e: anyhow::Error) -> Markup { | ||
html! { | ||
script { | ||
(PreEscaped("document.querySelector('#")) | ||
(error_type) | ||
(PreEscaped(r#"-container').classList.add("hidden"); | ||
document.querySelector('#"#)) | ||
(error_type) | ||
(PreEscaped(r#"-error').innerHTML = ` | ||
<div class='p-12 flex flex-col items-center align-middle justify-center text-center'> | ||
<h2 class='text-xl'> | ||
Error loading "#)) | ||
(error_type) | ||
(PreEscaped(r#" data - please refresh the page or try again later. | ||
</h2> | ||
<p>"#)) | ||
span class="font-mono" { | ||
"Error: " (e) | ||
} | ||
(PreEscaped("</p></div>`;")) | ||
} | ||
} | ||
} | ||
|
||
/// Handler to return the website's index | ||
pub async fn root( | ||
|
@@ -17,83 +45,65 @@ pub async fn root( | |
// Create a channel to stream content to client as we get it | ||
let (tx, rx) = mpsc::channel::<Result<String, Infallible>>(2); | ||
|
||
let context = Arc::new(Mutex::new(tera::Context::new())); | ||
let mut context = tera::Context::new(); | ||
|
||
let spot: Arc<Spot> = Arc::new(selected_spot.0.into()); | ||
|
||
// Add the initial context to the page for the loading state | ||
{ | ||
let mut context = context.lock().await; | ||
|
||
// Update with inital values. | ||
context.insert("spot", &*spot); | ||
context.insert("breaks", &state.breaks); | ||
#[cfg(debug_assertions)] | ||
context.insert("live_reload", &true); | ||
#[cfg(not(debug_assertions))] | ||
context.insert("live_reload", &false); | ||
|
||
tx.send(Ok(TEMPLATES.render("index.html", &context)?)) | ||
.await?; | ||
} | ||
context.insert("spot", &*spot); | ||
context.insert("breaks", &state.breaks); | ||
#[cfg(debug_assertions)] | ||
context.insert("live_reload", &true); | ||
#[cfg(not(debug_assertions))] | ||
context.insert("live_reload", &false); | ||
|
||
tx.send(Ok(TEMPLATES.render("index.html", &context)?)) | ||
.await?; | ||
|
||
let realtime_tx = tx.clone(); | ||
let realtime_context = context.clone(); | ||
let realtime_spot = spot.clone(); | ||
let realtime_state = state.clone(); | ||
tokio::spawn(async move { | ||
match Realtime::try_get(realtime_spot, realtime_state.realtime_url).await { | ||
Ok(realtime) => { | ||
let mut context = realtime_context.lock().await; | ||
context.insert("realtime_json", &serde_json::to_string(&realtime).unwrap()); | ||
|
||
realtime_tx | ||
.send(Ok(TEMPLATES.render("realtime.html", &context).unwrap())) | ||
.await | ||
.unwrap(); | ||
let html = html!( | ||
script type="application/json" id="realtime-data" {( | ||
PreEscaped( | ||
serde_json::to_string(&realtime).unwrap()) | ||
)} | ||
) | ||
.into(); | ||
|
||
realtime_tx.send(Ok(html)).await.unwrap(); | ||
} | ||
Err(e) => { | ||
let mut context = realtime_context.lock().await; | ||
context.insert("error", &e.to_string()); | ||
context.insert("error_type", &"latest"); | ||
context.insert("container", &"latest-container"); | ||
context.insert("error_container", &"latest-error"); | ||
realtime_tx | ||
.send(Ok(TEMPLATES.render("error.html", &context).unwrap())) | ||
.send(Ok(html!((error_markup("latest", e))).into())) | ||
.await | ||
.unwrap(); | ||
} | ||
} | ||
}); | ||
|
||
let water_quality_tx = tx.clone(); | ||
let water_quality_context = context.clone(); | ||
let water_quality_spot = spot.clone(); | ||
let water_quality_state = state.clone(); | ||
tokio::spawn(async move { | ||
match WaterQuality::try_get(water_quality_spot, water_quality_state.quality_url).await { | ||
Ok(water_quality) => { | ||
let mut context = water_quality_context.lock().await; | ||
context.insert( | ||
"water_quality_json", | ||
&serde_json::to_string(&water_quality).unwrap(), | ||
); | ||
|
||
water_quality_tx | ||
.send(Ok(TEMPLATES | ||
.render("water_quality.html", &context) | ||
.unwrap())) | ||
.await | ||
.unwrap(); | ||
let html = html!( | ||
script type="application/json" id="water-quality-data" {( | ||
PreEscaped( | ||
serde_json::to_string(&water_quality).unwrap()) | ||
)} | ||
) | ||
.into(); | ||
|
||
water_quality_tx.send(Ok(html)).await.unwrap(); | ||
} | ||
Err(e) => { | ||
let mut context = water_quality_context.lock().await; | ||
context.insert("error", &e.to_string()); | ||
context.insert("error_type", &"beach status"); | ||
context.insert("container", &"water-quality-container"); | ||
context.insert("error_container", &"water-quality-error"); | ||
water_quality_tx | ||
.send(Ok(TEMPLATES.render("error.html", &context).unwrap())) | ||
.send(Ok(html!((error_markup("water quality", e))).into())) | ||
.await | ||
.unwrap(); | ||
} | ||
|
@@ -103,22 +113,21 @@ pub async fn root( | |
tokio::spawn(async move { | ||
match Forecast::try_get(&spot, state.forecast_url).await { | ||
Ok(forecast) => { | ||
let mut context = context.lock().await; | ||
context.insert("forecast_json", &serde_json::to_string(&forecast).unwrap()); | ||
let html = html!( | ||
script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js" {} | ||
script type="application/json" id="forecast-data" {( | ||
PreEscaped( | ||
serde_json::to_string(&forecast).unwrap()) | ||
)} | ||
) | ||
.into(); | ||
|
||
tx.send(Ok(TEMPLATES.render("forecast.html", &context).unwrap())) | ||
.await | ||
.unwrap(); | ||
tx.send(Ok(html)).await.unwrap(); | ||
|
||
Ok(()) | ||
} | ||
Err(e) => { | ||
let mut context = context.lock().await; | ||
context.insert("error", &e.to_string()); | ||
context.insert("error_type", &"forecast"); | ||
context.insert("container", &"forecast-container"); | ||
context.insert("error_container", &"forecast-error"); | ||
tx.send(Ok(TEMPLATES.render("error.html", &context).unwrap())) | ||
tx.send(Ok(html!((error_markup("forecast", e))).into())) | ||
.await | ||
.unwrap(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
|
||
module.exports = { | ||
content: ["./templates/**/*.{html,js}"], | ||
content: ["./templates/**/*.{html,js}", "./src/routes/**"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,20 +17,20 @@ | |
<link href="https://api.fontshare.com/css?f[]=satoshi&display=swap" rel="stylesheet"> | ||
<link href="https://api.fontshare.com/css?f[]=array&display=swap" rel="stylesheet"> | ||
|
||
<link rel="preload" href="/assets/styles.css?version=93" as="style" /> | ||
<link rel="preload" href="/assets/styles.css?version=101" as="style" /> | ||
<link | ||
rel="preload" | ||
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js" | ||
as="script" | ||
/> | ||
<link | ||
rel="preload" | ||
href="/assets/static/index.min.js?version=96" | ||
href="/assets/static/index.min.js?version=104" | ||
as="script" | ||
/> | ||
|
||
<link | ||
href="/assets/styles.css?version=93" | ||
href="/assets/styles.css?version=101" | ||
rel="stylesheet" | ||
type="text/css" | ||
/> | ||
|
@@ -46,7 +46,7 @@ | |
@keyup.escape="showLiveFeed = false; showNav = false;" | ||
:class="{ 'overflow-hidden': showNav || showLiveFeed }" | ||
> | ||
<script src="/assets/static/index.min.js?version=96"></script> | ||
<script src="/assets/static/index.min.js?version=104"></script> | ||
{% block body %} {% endblock %} {% include "includes/footer.html" %} {% if | ||
live_reload %} | ||
<script> | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.