add search

This commit is contained in:
Vri 🌈 2023-10-11 21:18:18 +02:00
parent f6f179e071
commit 23cb696f61
Signed by: vrifox
SSH key fingerprint: SHA256:7OLOvW+jmULkXpdl5rUWgid7WJQhOIEgj+4WP/PtCpI
3 changed files with 41 additions and 7 deletions

View file

@ -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; use tera::Tera;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// build our application with a single route // 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 // run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
@ -15,10 +18,9 @@ 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 mut tera = Tera::new("templates/**/*").unwrap(); let tera = Tera::new("templates/**/*").unwrap();
// Prepare the context with some data // Prepare the context with some data
let mut context = tera::Context::new(); let context = tera::Context::new();
context.insert("name", "World");
// Render the template with the given context // Render the template with the given context
let rendered = tera.render("index.html.tera", &context).unwrap(); let rendered = tera.render("index.html.tera", &context).unwrap();
@ -28,3 +30,17 @@ async fn index() -> impl IntoResponse {
rendered, rendered,
) )
} }
async fn search(Query(params): Query<HashMap<String, String>>) -> 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,
)
}

View file

@ -3,11 +3,15 @@
<html> <html>
<head> <head>
<title>Very wichtig!</title> <title>Stadtratmonitor</title>
</head> </head>
<body> <body>
<h1>Henlo Welt!</h1> <h1>Stadtratmonitor</h1>
<form action="/search" method="get">
<input type="search" name="q" placeholder="Suchen">
<button>Suchen</button>
</form>
</body> </body>
</html> </html>

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Stadtratmonitor</title>
</head>
<body>
<h1>Stadtratmonitor</h1>
<p>{{ query }}</p>
</body>
</html>