Skip to content

Commit

Permalink
feat(examples): minijinja (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon authored Dec 9, 2023
1 parent c9675c9 commit 8c501f2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Here you can find a lot of small crabs 🦀.
* [markup](templates/markup)
* [tera](templates/tera)
* [maud](templates/maud)
* [minijinja](templates/minijinja)
* [Tracing aka logging](tracing)

## Usage
Expand Down
13 changes: 13 additions & 0 deletions examples/templates/minijinja/Cargo.toml
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"
68 changes: 68 additions & 0 deletions examples/templates/minijinja/src/main.rs
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}");
}
});
}
}
9 changes: 9 additions & 0 deletions examples/templates/minijinja/templates/index.html
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 %}
10 changes: 10 additions & 0 deletions examples/templates/minijinja/templates/layout.html
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>

0 comments on commit 8c501f2

Please sign in to comment.