From 4940b5dd5e792811491f1c3c3b1c70c8177c7e02 Mon Sep 17 00:00:00 2001 From: Michael Alyn Miller Date: Sat, 27 Nov 2021 20:59:51 -0800 Subject: Add Axum integration --- askama_axum/src/lib.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 askama_axum/src/lib.rs (limited to 'askama_axum/src/lib.rs') 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: &T, ext: &str) -> axum::http::Response> { + 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(), + } +} -- cgit