add initial template

This commit is contained in:
Vri 🌈 2023-10-11 20:53:35 +02:00
parent 3bf2af3d9c
commit f6f179e071
Signed by: vrifox
SSH key fingerprint: SHA256:7OLOvW+jmULkXpdl5rUWgid7WJQhOIEgj+4WP/PtCpI
4 changed files with 676 additions and 2 deletions

View file

@ -1,9 +1,10 @@
use axum::{routing::get, Router};
use axum::{http::header, response::IntoResponse, routing::get, Router};
use tera::Tera;
#[tokio::main]
async fn main() {
// build our application with a single route
let app = Router::new().route("/", get(|| async { "Hello, World!" }));
let app = Router::new().route("/", get(index));
// run it with hyper on localhost:3000
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
@ -11,3 +12,19 @@ async fn main() {
.await
.unwrap();
}
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,
)
}