aboutsummaryrefslogtreecommitdiffstats
path: root/askama_actix
diff options
context:
space:
mode:
Diffstat (limited to 'askama_actix')
-rw-r--r--askama_actix/Cargo.toml22
-rw-r--r--askama_actix/src/lib.rs22
-rw-r--r--askama_actix/templates/hello.html1
-rw-r--r--askama_actix/tests/basic.rs53
4 files changed, 98 insertions, 0 deletions
diff --git a/askama_actix/Cargo.toml b/askama_actix/Cargo.toml
new file mode 100644
index 0000000..1e857a5
--- /dev/null
+++ b/askama_actix/Cargo.toml
@@ -0,0 +1,22 @@
+[package]
+name = "askama_actix"
+version = "0.9.0"
+authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"]
+description = "Actix-Web integration for Askama templates"
+documentation = "https://docs.rs/askama"
+keywords = ["markup", "template", "jinja2", "html"]
+categories = ["template-engine"]
+homepage = "https://github.com/djc/askama"
+repository = "https://github.com/djc/askama"
+license = "MIT OR Apache-2.0"
+workspace = ".."
+edition = "2018"
+
+[dependencies]
+actix-web = { version = "2" }
+askama = { version = "0.9.0", path = "../askama", features = ["with-actix-web", "mime", "mime_guess"] }
+bytes = { version = "0.5" }
+futures = { version = "0.3" }
+
+[dev-dependencies]
+actix-rt = { version = "1", default-features = false }
diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs
new file mode 100644
index 0000000..7372034
--- /dev/null
+++ b/askama_actix/src/lib.rs
@@ -0,0 +1,22 @@
+pub use askama::*;
+use bytes::BytesMut;
+
+use actix_web::{error::ErrorInternalServerError, Error, HttpResponse};
+
+pub trait TemplateIntoResponse {
+ fn into_response(&self) -> ::std::result::Result<HttpResponse, Error>;
+}
+
+impl<T: askama::Template> TemplateIntoResponse for T {
+ fn into_response(&self) -> ::std::result::Result<HttpResponse, Error> {
+ let mut buffer = BytesMut::with_capacity(self.size_hint());
+ self.render_into(&mut buffer)
+ .map_err(|_| ErrorInternalServerError("Template parsing error"))?;
+
+ let ctype =
+ askama::mime::extension_to_mime_type(self.extension().unwrap_or("txt")).to_string();
+ Ok(HttpResponse::Ok()
+ .content_type(ctype.as_str())
+ .body(buffer.freeze()))
+ }
+}
diff --git a/askama_actix/templates/hello.html b/askama_actix/templates/hello.html
new file mode 100644
index 0000000..8149be7
--- /dev/null
+++ b/askama_actix/templates/hello.html
@@ -0,0 +1 @@
+Hello, {{ name }}!
diff --git a/askama_actix/tests/basic.rs b/askama_actix/tests/basic.rs
new file mode 100644
index 0000000..f4beff7
--- /dev/null
+++ b/askama_actix/tests/basic.rs
@@ -0,0 +1,53 @@
+use actix_web::http::header::CONTENT_TYPE;
+use actix_web::test;
+use actix_web::web;
+use askama_actix::{Template, TemplateIntoResponse};
+use bytes::Bytes;
+
+#[derive(Template)]
+#[template(path = "hello.html")]
+struct HelloTemplate<'a> {
+ name: &'a str,
+}
+
+#[actix_rt::test]
+async fn test_actix_web() {
+ let srv = test::start(|| {
+ actix_web::App::new()
+ .service(web::resource("/").to(|| async { HelloTemplate { name: "world" } }))
+ });
+
+ let request = srv.get("/");
+ let mut response = request.send().await.unwrap();
+ assert!(response.status().is_success());
+ assert_eq!(
+ response.headers().get(CONTENT_TYPE).unwrap(),
+ "text/html; charset=utf-8"
+ );
+
+ let bytes = response.body().await.unwrap();
+ assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
+}
+
+#[actix_rt::test]
+async fn test_actix_web_responder() {
+ let srv = test::start(|| {
+ actix_web::App::new().service(web::resource("/").to(|| {
+ async {
+ let name = "world".to_owned();
+ HelloTemplate { name: &name }.into_response()
+ }
+ }))
+ });
+
+ let request = srv.get("/");
+ let mut response = request.send().await.unwrap();
+ assert!(response.status().is_success());
+ assert_eq!(
+ response.headers().get(CONTENT_TYPE).unwrap(),
+ "text/html; charset=utf-8"
+ );
+
+ let bytes = response.body().await.unwrap();
+ assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
+}