aboutsummaryrefslogtreecommitdiffstats
path: root/askama_axum
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--askama_axum/Cargo.toml23
l---------askama_axum/LICENSE-APACHE1
l---------askama_axum/LICENSE-MIT1
-rw-r--r--askama_axum/README.md9
-rw-r--r--askama_axum/src/lib.rs25
-rw-r--r--askama_axum/templates/hello.html1
-rw-r--r--askama_axum/tests/basic.rs35
7 files changed, 95 insertions, 0 deletions
diff --git a/askama_axum/Cargo.toml b/askama_axum/Cargo.toml
new file mode 100644
index 0000000..7d3045d
--- /dev/null
+++ b/askama_axum/Cargo.toml
@@ -0,0 +1,23 @@
+[package]
+name = "askama_axum"
+version = "0.10.0"
+authors = ["Michael Alyn Miller <malyn@strangeGizmo.com>"]
+edition = "2018"
+description = "Axum integration for Askama templates"
+keywords = ["markup", "template", "jinja2", "html", "axum"]
+categories = ["template-engine"]
+homepage = "https://github.com/djc/askama"
+repository = "https://github.com/djc/askama"
+documentation = "https://docs.rs/askama"
+license = "MIT OR Apache-2.0"
+workspace = ".."
+readme = "README.md"
+
+[dependencies]
+askama = { version = "0.11.0-beta.1", path = "../askama", default-features = false, features = ["with-axum", "mime", "mime_guess"] }
+axum = { version = "0.3", default-features = false }
+
+[dev-dependencies]
+hyper = { version = "0.14", features = ["full"] }
+tokio = { version = "1.0", features = ["full"] }
+tower = { version = "0.4", features = ["util"] } \ No newline at end of file
diff --git a/askama_axum/LICENSE-APACHE b/askama_axum/LICENSE-APACHE
new file mode 120000
index 0000000..965b606
--- /dev/null
+++ b/askama_axum/LICENSE-APACHE
@@ -0,0 +1 @@
+../LICENSE-APACHE \ No newline at end of file
diff --git a/askama_axum/LICENSE-MIT b/askama_axum/LICENSE-MIT
new file mode 120000
index 0000000..76219eb
--- /dev/null
+++ b/askama_axum/LICENSE-MIT
@@ -0,0 +1 @@
+../LICENSE-MIT \ No newline at end of file
diff --git a/askama_axum/README.md b/askama_axum/README.md
new file mode 100644
index 0000000..584dbd9
--- /dev/null
+++ b/askama_axum/README.md
@@ -0,0 +1,9 @@
+# askama_axum: Askama integration with Axum
+
+[![Documentation](https://docs.rs/askama_axum/badge.svg)](https://docs.rs/askama_axum/)
+[![Latest version](https://img.shields.io/crates/v/askama_axum.svg)](https://crates.io/crates/askama_axum)
+[![Build Status](https://github.com/djc/askama/workflows/CI/badge.svg)](https://github.com/djc/askama/actions?query=workflow%3ACI)
+[![Chat](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/djc/askama)
+
+Integration of the [Askama](https://github.com/djc/askama) templating engine in
+code building on the Axum web framework.
diff --git a/askama_axum/src/lib.rs b/askama_axum/src/lib.rs
new file mode 100644
index 0000000..0c21464
--- /dev/null
+++ b/askama_axum/src/lib.rs
@@ -0,0 +1,25 @@
+#![deny(elided_lifetimes_in_paths)]
+
+pub use askama::*;
+use axum::{
+ self,
+ body::{Bytes, Full},
+ http::{Response, StatusCode},
+};
+
+pub fn into_response<T: Template>(t: &T, ext: &str) -> axum::http::Response<Full<Bytes>> {
+ match t.render() {
+ Ok(body) => Response::builder()
+ .status(StatusCode::OK)
+ .header(
+ "content-type",
+ askama::mime::extension_to_mime_type(ext).to_string(),
+ )
+ .body(body.into())
+ .unwrap(),
+ Err(_) => Response::builder()
+ .status(StatusCode::INTERNAL_SERVER_ERROR)
+ .body(vec![].into())
+ .unwrap(),
+ }
+}
diff --git a/askama_axum/templates/hello.html b/askama_axum/templates/hello.html
new file mode 100644
index 0000000..8149be7
--- /dev/null
+++ b/askama_axum/templates/hello.html
@@ -0,0 +1 @@
+Hello, {{ name }}!
diff --git a/askama_axum/tests/basic.rs b/askama_axum/tests/basic.rs
new file mode 100644
index 0000000..acd53b7
--- /dev/null
+++ b/askama_axum/tests/basic.rs
@@ -0,0 +1,35 @@
+use askama::Template;
+use axum::{
+ body::Body,
+ http::{Request, StatusCode},
+ routing::get,
+ Router,
+};
+use tower::util::ServiceExt;
+
+#[derive(Template)]
+#[template(path = "hello.html")]
+struct HelloTemplate<'a> {
+ name: &'a str,
+}
+
+async fn hello() -> HelloTemplate<'static> {
+ HelloTemplate { name: "world" }
+}
+
+#[tokio::test]
+async fn template_to_response() {
+ let app = Router::new().route("/", get(hello));
+
+ let res = app
+ .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
+ .await
+ .unwrap();
+ assert_eq!(res.status(), StatusCode::OK);
+
+ let headers = res.headers();
+ assert_eq!(headers["Content-Type"], "text/html; charset=utf-8");
+
+ let body = hyper::body::to_bytes(res.into_body()).await.unwrap();
+ assert_eq!(&body[..], b"Hello, world!");
+}