From 23cb696f613acca3fa680c919751dbfb928df2a7 Mon Sep 17 00:00:00 2001 From: Vri Date: Wed, 11 Oct 2023 21:18:18 +0200 Subject: [PATCH] add search --- src/main.rs | 26 +++++++++++++++++++++----- templates/index.html.tera | 8 ++++++-- templates/search.html.tera | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 templates/search.html.tera diff --git a/src/main.rs b/src/main.rs index 44f5032..522796a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,13 @@ -use axum::{http::header, response::IntoResponse, routing::get, Router}; +use axum::{extract::Query, http::header, response::IntoResponse, routing::get, Router}; +use std::collections::HashMap; use tera::Tera; #[tokio::main] async fn main() { // build our application with a single route - let app = Router::new().route("/", get(index)); + let app = Router::new() + .route("/", get(index)) + .route("/search", get(search)); // run it with hyper on localhost:3000 axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) @@ -15,10 +18,9 @@ async fn main() { async fn index() -> impl IntoResponse { // Create a new Tera instance and add a template from a string - let mut tera = Tera::new("templates/**/*").unwrap(); + let tera = Tera::new("templates/**/*").unwrap(); // Prepare the context with some data - let mut context = tera::Context::new(); - context.insert("name", "World"); + let context = tera::Context::new(); // Render the template with the given context let rendered = tera.render("index.html.tera", &context).unwrap(); @@ -28,3 +30,17 @@ async fn index() -> impl IntoResponse { rendered, ) } + +async fn search(Query(params): Query>) -> impl IntoResponse { + let tera = Tera::new("templates/**/*").unwrap(); + // Prepare the context with some data + let mut context = tera::Context::new(); + context.insert("query", params.get("q").unwrap()); + + let rendered = tera.render("search.html.tera", &context).unwrap(); + + ( + [(header::CONTENT_TYPE, "text/html; charset=utf-8")], + rendered, + ) +} diff --git a/templates/index.html.tera b/templates/index.html.tera index ce4a81e..6f44759 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -3,11 +3,15 @@ - Very wichtig! + Stadtratmonitor -

Henlo Welt!

+

Stadtratmonitor

+
+ + +
\ No newline at end of file diff --git a/templates/search.html.tera b/templates/search.html.tera new file mode 100644 index 0000000..2dd9442 --- /dev/null +++ b/templates/search.html.tera @@ -0,0 +1,14 @@ + + + + + + Stadtratmonitor + + + +

Stadtratmonitor

+

{{ query }}

+ + + \ No newline at end of file