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)) .route("/search", get(search)); // run it with hyper on localhost:3000 axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) .serve(app.into_make_service()) .await .unwrap(); } async fn index() -> impl IntoResponse { // Create a new Tera instance and add a template from a string let tera = Tera::new("templates/**/*").unwrap(); // Prepare the context with some data let context = tera::Context::new(); // 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, ) } 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, ) }