From b85049f9f7842ebee7b470b13282aa6188841a60 Mon Sep 17 00:00:00 2001 From: vrifox Date: Thu, 12 Oct 2023 18:31:22 +0200 Subject: [PATCH] rework tera instantiation --- src/main.rs | 16 ++++++++++++++-- templates/{index.html.tera => index.html} | 0 templates/{search.html.tera => search.html} | 0 3 files changed, 14 insertions(+), 2 deletions(-) rename templates/{index.html.tera => index.html} (100%) rename templates/{search.html.tera => search.html} (100%) diff --git a/src/main.rs b/src/main.rs index 3b5e160..a4489c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ async fn main() { async fn index() -> impl IntoResponse { // Create a new Tera instance and add a template from a string - let tera = Tera::new("templates/**/*").unwrap(); + let tera = tera_create(); // Prepare the context with some data let context = tera::Context::new(); @@ -29,7 +29,7 @@ async fn index() -> impl IntoResponse { } async fn search(Query(params): Query>) -> impl IntoResponse { - let tera = Tera::new("templates/**/*").unwrap(); + let tera = tera_create(); // Prepare the context with some data let mut context = tera::Context::new(); context.insert("query", params.get("q").unwrap()); @@ -39,6 +39,18 @@ async fn search(Query(params): Query>) -> impl IntoRespo header(rendered) } +fn tera_create() -> Tera { + let tera = match Tera::new("templates/**/*.html") { + Ok(tera) => tera, + Err(error) => { + println!("Parsing error(s): {}", error); + ::std::process::exit(1); + } + }; + + tera +} + fn header(rendered: impl ToString) -> impl IntoResponse { ( [(header::CONTENT_TYPE, "text/html; charset=utf-8")], diff --git a/templates/index.html.tera b/templates/index.html similarity index 100% rename from templates/index.html.tera rename to templates/index.html diff --git a/templates/search.html.tera b/templates/search.html similarity index 100% rename from templates/search.html.tera rename to templates/search.html