diff --git a/src/main.rs b/src/main.rs index b466cb7..dd918bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use axum::{ }; use dotenv::dotenv; use libsql::{Builder, Connection}; -use minijinja::{path_loader, Environment}; +use minijinja::{path_loader, AutoEscape, Environment}; use std::{env, net::SocketAddr, sync::Arc, time::Duration}; use tower::ServiceBuilder; use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer}; @@ -78,6 +78,7 @@ async fn main() { let mut jinja = Environment::new(); jinja.set_loader(path_loader("templates")); jinja.add_filter("date_string", date_string); + jinja.set_auto_escape_callback(|_| AutoEscape::Html); let _ = SHARED_JINJA_ENV.set(jinja.clone()); // Setup static file service diff --git a/src/render_html.rs b/src/render_html.rs index b2e5638..ceb5e4f 100644 --- a/src/render_html.rs +++ b/src/render_html.rs @@ -26,15 +26,18 @@ pub fn render_block(template_name: &str, block_name: &str, context } } -// TODO: Improve error handling -pub fn render_html_str(template_raw: &str, context: S) -> Result, Box> { - let template = SHARED_JINJA_ENV +pub fn render_str(template_raw: &str, context: S) -> Option> { + match SHARED_JINJA_ENV .get() .expect("Jinja environment not initialized!") - .render_str(template_raw, context)?; - // Minijiinja does not escape html when using render() - let str = v_htmlescape::escape(template.as_str()); - return Ok(Html(str.to_string())); + .render_str(template_raw, context) + { + Ok(str) => Some(Html(str)), + Err(err) => { + println!("Error rendering string: {}", err); + return Html(String::from("Woopsie! Something broke!")).into(); + } + } } fn render( diff --git a/src/routes/todos/create_todo.rs b/src/routes/todos/create_todo.rs index 4c7ae2d..b254e84 100644 --- a/src/routes/todos/create_todo.rs +++ b/src/routes/todos/create_todo.rs @@ -1,6 +1,6 @@ use crate::{ models::{todo_item::TodoItemRequest, user::User}, - render_html::render_html_str, + render_html::render_str, AppState, }; use axum::{extract::State, http::StatusCode, response::Html, Form}; @@ -30,7 +30,7 @@ pub async fn create_todo( return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Unknown error"))); } - return Ok(render_html_str( + return Ok(render_str( "Todo item '{{ title_clone }}' succesfuly added!", context! { title_clone