rework tera instantiation

This commit is contained in:
Vri 🌈 2023-10-12 18:31:22 +02:00
parent 8ee0f38bc2
commit b85049f9f7
Signed by: vrifox
SSH key fingerprint: SHA256:7OLOvW+jmULkXpdl5rUWgid7WJQhOIEgj+4WP/PtCpI
3 changed files with 14 additions and 2 deletions

View file

@ -18,7 +18,7 @@ async fn main() {
async fn index() -> impl IntoResponse { async fn index() -> impl IntoResponse {
// Create a new Tera instance and add a template from a string // 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 // Prepare the context with some data
let context = tera::Context::new(); let context = tera::Context::new();
@ -29,7 +29,7 @@ async fn index() -> impl IntoResponse {
} }
async fn search(Query(params): Query<HashMap<String, String>>) -> impl IntoResponse { async fn search(Query(params): Query<HashMap<String, String>>) -> impl IntoResponse {
let tera = Tera::new("templates/**/*").unwrap(); let tera = tera_create();
// Prepare the context with some data // Prepare the context with some data
let mut context = tera::Context::new(); let mut context = tera::Context::new();
context.insert("query", params.get("q").unwrap()); context.insert("query", params.get("q").unwrap());
@ -39,6 +39,18 @@ async fn search(Query(params): Query<HashMap<String, String>>) -> impl IntoRespo
header(rendered) 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 { fn header(rendered: impl ToString) -> impl IntoResponse {
( (
[(header::CONTENT_TYPE, "text/html; charset=utf-8")], [(header::CONTENT_TYPE, "text/html; charset=utf-8")],