From 054a620664786c05dcc78a4fb84f298e9cc3de7f Mon Sep 17 00:00:00 2001 From: traxys Date: Tue, 15 Aug 2023 19:46:26 +0200 Subject: [PATCH] Add internal error template --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 29 ++++++++++++++++++++++------- templates/error.html | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 templates/error.html diff --git a/Cargo.lock b/Cargo.lock index f25eef5..d3c8015 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1121,6 +1121,7 @@ dependencies = [ "axum", "color-eyre", "envious", + "once_cell", "openidconnect", "parking_lot", "serde", diff --git a/Cargo.toml b/Cargo.toml index 5bb4442..69db301 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" axum = { version = "0.6.20", features = ["query"] } color-eyre = "0.6.2" envious = "0.2.2" +once_cell = "1.18.0" openidconnect = "3.3.0" parking_lot = "0.12.1" serde = { version = "1.0.183", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index e16c599..db21eb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use axum::{ Router, }; use color_eyre::eyre; +use once_cell::sync::Lazy; use openidconnect::{ core::{CoreAuthenticationFlow, CoreClient, CoreProviderMetadata}, url::Url, @@ -263,7 +264,6 @@ impl OpenidConnector { struct AppState { db: PgPool, oidc: OpenidConnector, - tera: Tera, } #[derive(thiserror::Error, Debug)] @@ -280,7 +280,17 @@ struct InternalError; impl IntoResponse for InternalError { fn into_response(self) -> axum::response::Response { - (StatusCode::INTERNAL_SERVER_ERROR, "Internal Error").into_response() + ( + StatusCode::INTERNAL_SERVER_ERROR, + match TEMPLATES.render("error.html", &global_context()) { + Ok(v) => Html(v).into_response(), + Err(e) => { + tracing::error!("Could not generate internal error: {e:?}"); + "Internal Error".into_response() + } + }, + ) + .into_response() } } @@ -353,13 +363,20 @@ fn global_context() -> tera::Context { ctx } -async fn page_not_found(state: State>) -> Result<(StatusCode, Html), Error> { +async fn page_not_found() -> Result<(StatusCode, Html), Error> { Ok(( StatusCode::NOT_FOUND, - Html(state.tera.render("not_found.html", &global_context())?), + Html(TEMPLATES.render("not_found.html", &global_context())?), )) } +async fn error() -> Error { + Error::InternalError +} + +pub static TEMPLATES: Lazy = + Lazy::new(|| Tera::new("templates/*.html").expect("Could not generate templates")); + #[tokio::main] async fn main() -> color_eyre::Result<()> { color_eyre::install()?; @@ -384,15 +401,13 @@ async fn main() -> color_eyre::Result<()> { sqlx::migrate!().run(&db).await?; - let tera = Tera::new("templates/*.html")?; - tracing::info!("Listening on {addr}"); let router = Router::new() .route("/login", get(login)) .route("/login/redirect/:id", get(redirected)) .fallback(page_not_found) - .with_state(Arc::new(AppState { db, oidc, tera })); + .with_state(Arc::new(AppState { db, oidc })); Ok(axum::Server::bind(&addr) .serve(router.into_make_service()) diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..01b85ff --- /dev/null +++ b/templates/error.html @@ -0,0 +1,19 @@ + + + + + + {{ title }} + + + +
+
+

Internal Error

+
+
+ +