From 054d22e945216a4019dc79bccab1f27063324342 Mon Sep 17 00:00:00 2001 From: Neikos Date: Mon, 9 Aug 2021 17:22:12 +0200 Subject: [PATCH 1/6] Add axum support --- maud/Cargo.toml | 1 + maud/src/lib.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/maud/Cargo.toml b/maud/Cargo.toml index eb04fb54..ed67fef1 100644 --- a/maud/Cargo.toml +++ b/maud/Cargo.toml @@ -25,6 +25,7 @@ rocket = { version = ">= 0.3, < 0.5", optional = true } futures-util = { version = "0.3.0", optional = true, default-features = false } actix-web-dep = { package = "actix-web", version = ">= 2, < 4", optional = true, default-features = false } tide = { version = "0.16.0", optional = true } +axum = { version = "0.1.3", optional = true } [dev-dependencies] trybuild = { version = "1.0.33", features = ["diff"] } diff --git a/maud/src/lib.rs b/maud/src/lib.rs index 96d7c9da..b1af57f5 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -253,3 +253,26 @@ mod tide_support { } } } + +#[cfg(feature = "axum")] +mod axum_support { + use crate::PreEscaped; + use alloc::string::String; + use axum::{ + body::Body, + http::{HeaderValue, Response, StatusCode}, + response::IntoResponse, + }; + + impl IntoResponse for PreEscaped { + fn into_response(self) -> Response { + let mut res = Response::new(Body::from(self.0)); + *res.status_mut() = StatusCode::OK; + res.headers_mut().insert( + axum::http::header::CONTENT_TYPE, + HeaderValue::from_static("text/html; charset=utf-8"), + ); + res + } + } +} From 45a5514b2b0fbd316c8da9bdca509a5168bed68f Mon Sep 17 00:00:00 2001 From: Neikos Date: Mon, 9 Aug 2021 17:27:46 +0200 Subject: [PATCH 2/6] Update documentation --- docs/content/web-frameworks.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/content/web-frameworks.md b/docs/content/web-frameworks.md index 0eb52d0a..d3e14390 100644 --- a/docs/content/web-frameworks.md +++ b/docs/content/web-frameworks.md @@ -177,3 +177,34 @@ async fn main() -> tide::Result<()> { Ok(()) } ``` + +# Axum + +Axum support is available with the "axum" feature: + +```toml +# ... +[dependencies] +maud = { version = "*", features = ["axum"] } +# ... +``` + +This adds an implementation of `IntoResponse` for `Markup`/`PreEscaped`. +This then allows you to use it directly as a response! + +```rust,no_run +use maud::html; +use axum::prelude::*; + +#[tokio::main] +async fn main() { + // build our application with a single route + let app = route("/", get(|| async { html! { h1 { "Hello, World!" } } })); + + // run it with hyper on localhost:3000 + axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) + .serve(app.into_make_service()) + .await + .unwrap(); +} +``` \ No newline at end of file From 68161aa8392b86b55c7eacddc2abf06ea0178747 Mon Sep 17 00:00:00 2001 From: Neikos Date: Mon, 9 Aug 2021 20:10:57 +0200 Subject: [PATCH 3/6] Add axum to doctests --- docs/content/web-frameworks.md | 10 ++++++++-- doctest/Cargo.toml | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/content/web-frameworks.md b/docs/content/web-frameworks.md index d3e14390..a48d8ca8 100644 --- a/docs/content/web-frameworks.md +++ b/docs/content/web-frameworks.md @@ -193,13 +193,19 @@ This adds an implementation of `IntoResponse` for `Markup`/`PreEscaped`. This then allows you to use it directly as a response! ```rust,no_run -use maud::html; +use maud::{html, Markup}; use axum::prelude::*; +async fn hello_world() -> Markup { + html! { + h1 { "Hello, World!" } + } +} + #[tokio::main] async fn main() { // build our application with a single route - let app = route("/", get(|| async { html! { h1 { "Hello, World!" } } })); + let app = route("/", get(hello_world)); // run it with hyper on localhost:3000 axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) diff --git a/doctest/Cargo.toml b/doctest/Cargo.toml index 25d575ff..a06e4cf3 100644 --- a/doctest/Cargo.toml +++ b/doctest/Cargo.toml @@ -8,11 +8,13 @@ edition = "2018" actix-web = "3" ammonia = "3" iron = "0.6" -maud = { path = "../maud", features = ["actix-web", "iron", "rocket", "tide"] } +maud = { path = "../maud", features = ["actix-web", "iron", "rocket", "tide", "axum"] } pulldown-cmark = "0.8" rocket = "0.4" rouille = "3" tide = "0.16" +tokio = { version = "1.9.0", features = ["rt", "macros", "rt-multi-thread"] } +axum = "0.1.3" [dependencies.async-std] version = "1.9.0" From 8c3a2724aabbb7878716b2eb0f2e32aa39fe80ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sun, 15 Aug 2021 17:12:32 +0000 Subject: [PATCH 4/6] Update maud/src/lib.rs Co-authored-by: Chris Wong --- maud/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maud/src/lib.rs b/maud/src/lib.rs index b1af57f5..f1b94e5d 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -265,7 +265,7 @@ mod axum_support { }; impl IntoResponse for PreEscaped { - fn into_response(self) -> Response { + fn into_response(self) -> Response { let mut res = Response::new(Body::from(self.0)); *res.status_mut() = StatusCode::OK; res.headers_mut().insert( From 8ddb08cd195eb3719c740bc628e239c33630751a Mon Sep 17 00:00:00 2001 From: Neikos Date: Sun, 15 Aug 2021 19:14:46 +0200 Subject: [PATCH 5/6] Import header module --- maud/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maud/src/lib.rs b/maud/src/lib.rs index f1b94e5d..b30e1e32 100644 --- a/maud/src/lib.rs +++ b/maud/src/lib.rs @@ -260,7 +260,7 @@ mod axum_support { use alloc::string::String; use axum::{ body::Body, - http::{HeaderValue, Response, StatusCode}, + http::{header, HeaderValue, Response, StatusCode}, response::IntoResponse, }; @@ -269,7 +269,7 @@ mod axum_support { let mut res = Response::new(Body::from(self.0)); *res.status_mut() = StatusCode::OK; res.headers_mut().insert( - axum::http::header::CONTENT_TYPE, + header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"), ); res From 46cc6c933e19f7b4f7764f5e21b33403e60511c4 Mon Sep 17 00:00:00 2001 From: Neikos Date: Sun, 15 Aug 2021 19:15:37 +0200 Subject: [PATCH 6/6] Add Changelog Entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9364c499..d41ae026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ [#278](https://github.com/lambda-fairy/maud/issues/278) - Provide Tide support. [#280](https://github.com/lambda-fairy/maud/pull/280) +- Provide Axum support. + [#284](https://github.com/lambda-fairy/maud/pull/284) ## [0.22.2] - 2021-01-09