-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "template-minijinja" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
viz.workspace = true | ||
|
||
serde.workspace = true | ||
tokio = { workspace = true, features = [ "rt-multi-thread", "macros" ] } | ||
minijinja = { version = "1", features = ["loader"] } | ||
once_cell = "1.19" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#![deny(warnings)] | ||
#![allow(clippy::unused_async)] | ||
|
||
use std::{net::SocketAddr, sync::Arc}; | ||
|
||
use minijinja::{context, path_loader, Environment}; | ||
use once_cell::sync::Lazy; | ||
use serde::Serialize; | ||
use tokio::net::TcpListener; | ||
use viz::{serve, BytesMut, Error, Request, Response, ResponseExt, Result, Router, Tree}; | ||
|
||
static MINIJINJA: Lazy<Environment> = Lazy::new(|| { | ||
let mut env = Environment::new(); | ||
env.set_loader(path_loader("examples/templates/minijinja/templates")); | ||
env | ||
}); | ||
|
||
#[derive(Serialize)] | ||
struct User<'a> { | ||
url: &'a str, | ||
username: &'a str, | ||
} | ||
|
||
async fn index(_: Request) -> Result<Response> { | ||
let mut buf = BytesMut::with_capacity(512); | ||
buf.extend( | ||
MINIJINJA | ||
.get_template("index.html") | ||
.map_err(Error::normal)? | ||
.render(context! { | ||
title => "Viz.rs", | ||
users => &vec![ | ||
User { | ||
url: "https://github.com/rust-lang", | ||
username: "rust-lang", | ||
}, | ||
User { | ||
url: "https://github.com/viz-rs", | ||
username: "viz-rs", | ||
}, | ||
], | ||
}) | ||
.map_err(Error::normal)? | ||
.as_bytes(), | ||
); | ||
|
||
Ok(Response::html(buf.freeze())) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("listening on http://{addr}"); | ||
|
||
let app = Router::new().get("/", index); | ||
let tree = Arc::new(Tree::from(app)); | ||
|
||
loop { | ||
let (stream, addr) = listener.accept().await?; | ||
let tree = tree.clone(); | ||
tokio::task::spawn(async move { | ||
if let Err(err) = serve(stream, tree, Some(addr)).await { | ||
eprintln!("Error while serving HTTP connection: {err}"); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends "layout.html" %} | ||
{% block title %} Viz & MiniJinja {% endblock title %} | ||
{% block body %} | ||
<ul> | ||
{% for user in users %} | ||
<li><a href="{{ user.url | safe }}">{{ user.username }}</a></li> | ||
{% endfor %} | ||
</ul> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
<title> | ||
{% block title %}{% endblock %} | ||
</title> | ||
</head> | ||
<body> | ||
{% block body %}{% endblock %} | ||
</body> | ||
</html> |