2023-10-11 20:53:35 +02:00
|
|
|
use axum::{http::header, response::IntoResponse, routing::get, Router};
|
|
|
|
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 20:53:35 +02:00
|
|
|
let app = Router::new().route("/", get(index));
|
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
|
|
|
|
let mut tera = Tera::new("templates/**/*").unwrap();
|
|
|
|
// Prepare the context with some data
|
|
|
|
let mut context = tera::Context::new();
|
|
|
|
context.insert("name", "World");
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
)
|
|
|
|
}
|