add initial template
This commit is contained in:
parent
3bf2af3d9c
commit
f6f179e071
4 changed files with 676 additions and 2 deletions
21
src/main.rs
21
src/main.rs
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue