stadtratmonitor-rust/src/main.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

2023-10-11 21:18:18 +02:00
use axum::{extract::Query, http::header, response::IntoResponse, routing::get, Router};
use std::collections::HashMap;
2023-10-11 20:53:35 +02:00
use tera::Tera;
2023-10-11 20:23:13 +02:00
#[tokio::main]
async fn main() {
// build our application with a single route
2023-10-11 21:18:18 +02:00
let app = Router::new()
.route("/", get(index))
.route("/search", get(search));
2023-10-11 20:23:13 +02:00
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
2023-10-11 20:12:24 +02:00
}
2023-10-11 20:53:35 +02:00
async fn index() -> impl IntoResponse {
// Create a new Tera instance and add a template from a string
2023-10-11 21:18:18 +02:00
let tera = Tera::new("templates/**/*").unwrap();
2023-10-11 20:53:35 +02:00
// Prepare the context with some data
2023-10-11 21:18:18 +02:00
let context = tera::Context::new();
2023-10-11 20:53:35 +02:00
// Render the template with the given context
let rendered = tera.render("index.html.tera", &context).unwrap();
(
[(header::CONTENT_TYPE, "text/html; charset=utf-8")],
rendered,
)
}
2023-10-11 21:18:18 +02:00
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,
)
}