add search
This commit is contained in:
parent
f6f179e071
commit
23cb696f61
3 changed files with 41 additions and 7 deletions
26
src/main.rs
26
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<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,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -3,11 +3,15 @@
|
|||
<html>
|
||||
|
||||
<head>
|
||||
<title>Very wichtig!</title>
|
||||
<title>Stadtratmonitor</title>
|
||||
</head>
|
||||
|
||||
<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>
|
||||
|
||||
</html>
|
14
templates/search.html.tera
Normal file
14
templates/search.html.tera
Normal file
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Stadtratmonitor</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Stadtratmonitor</h1>
|
||||
<p>{{ query }}</p>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in a new issue