diff --git a/nixie-server/src/main.rs b/nixie-server/src/main.rs index f23cd45..e2a7493 100644 --- a/nixie-server/src/main.rs +++ b/nixie-server/src/main.rs @@ -2,7 +2,9 @@ use std::sync::LazyLock; use std::time::Duration; use axum::Router; +use axum::http::StatusCode; use axum::response::Html; +use axum::response::IntoResponse; use axum::routing::get; use axum_login::AuthManagerLayerBuilder; use axum_login::AuthnBackend; @@ -16,7 +18,6 @@ use sqlx::SqlitePool; use tera::Context; use tera::Tera; use thiserror::Error; -use tokio::sync::RwLock; use tokio::task; use tokio::task::AbortHandle; use tower_livereload::LiveReloadLayer; @@ -28,6 +29,29 @@ use tracing_subscriber::EnvFilter; pub mod users; +pub type WebResult = Result; + +#[derive(Debug, Error, Display)] +pub enum AppError { + /// An error occurred while templating + Tera(#[from] tera::Error), +} + +impl IntoResponse for AppError { + fn into_response(self) -> axum::response::Response { + let mut error_context = Context::new(); + error_context.insert("error", &self.to_string()); + ( + StatusCode::INTERNAL_SERVER_ERROR, + Html( + render_template("internal_error.tera.html", &error_context) + .unwrap_or_else(|_| "ERROR RENDERING ERROR! FATAL".to_string()), + ), + ) + .into_response() + } +} + #[tokio::main] async fn main() -> anyhow::Result<()> { run().await @@ -93,7 +117,7 @@ pub struct AppState { pub db: SqlitePool, } -pub static TERA: LazyLock> = +pub static TERA: LazyLock> = LazyLock::new(|| Tera::new("templates/**.tera.html").unwrap().into()); async fn run() -> anyhow::Result<()> { @@ -140,7 +164,7 @@ async fn run() -> anyhow::Result<()> { .iter() .any(|ev| !matches!(ev.event.kind, EventKind::Access(_))) { - match TERA.blocking_write().full_reload() { + match TERA.write().unwrap().full_reload() { Ok(()) => { reloader.reload(); } @@ -192,14 +216,10 @@ async fn shutdown_signal(handle: AbortHandle) { handle.abort(); } -pub async fn render_template(name: &str, context: &Context) -> tera::Result { - TERA.read().await.render(name, context) +pub fn render_template(name: &str, context: &Context) -> tera::Result { + TERA.read().unwrap().render(name, context) } async fn show_protected() -> Html { - Html( - render_template("protected.tera.html", &Context::default()) - .await - .unwrap(), - ) + Html(render_template("protected.tera.html", &Context::default()).unwrap()) } diff --git a/nixie-server/src/users/login.rs b/nixie-server/src/users/login.rs index 4dfb6d9..9e45694 100644 --- a/nixie-server/src/users/login.rs +++ b/nixie-server/src/users/login.rs @@ -3,26 +3,15 @@ use axum::http::StatusCode; use axum::response::Html; use axum::response::IntoResponse; use axum::response::Redirect; +use tera::Context; use crate::AuthSession; use crate::UserCredentials; +use crate::WebResult; +use crate::render_template; -pub async fn show_login() -> Html { - format!( - r##" - - - -
- - - -
- - - "## - ) - .into() +pub async fn show_login() -> WebResult> { + Ok(render_template("users/login.tera.html", &Context::default()).map(Html)?) } pub async fn do_login( diff --git a/nixie-server/src/users/register.rs b/nixie-server/src/users/register.rs index 030f6ab..82e7fbb 100644 --- a/nixie-server/src/users/register.rs +++ b/nixie-server/src/users/register.rs @@ -4,26 +4,15 @@ use axum::response::Html; use axum::response::IntoResponse; use axum::response::Redirect; use password_auth::generate_hash; +use tera::Context; use crate::AppState; use crate::UserCredentials; +use crate::WebResult; +use crate::render_template; -pub async fn show_register() -> Html { - format!( - r##" - - - -
- - - -
- - - "## - ) - .into() +pub async fn show_register() -> WebResult> { + Ok(render_template("users/register.tera.html", &Context::default()).map(Html)?) } pub async fn do_register( diff --git a/nixie-server/templates/base.tera.html b/nixie-server/templates/base.tera.html index edfecd0..c0b50f0 100644 --- a/nixie-server/templates/base.tera.html +++ b/nixie-server/templates/base.tera.html @@ -11,20 +11,24 @@ -