Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add axum support #284

Merged
merged 6 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions docs/content/web-frameworks.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,40 @@ 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<String>`.
This then allows you to use it directly as a response!

```rust,no_run
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(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();
}
```
4 changes: 3 additions & 1 deletion doctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions maud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
23 changes: 23 additions & 0 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{header, HeaderValue, Response, StatusCode},
response::IntoResponse,
};

impl IntoResponse for PreEscaped<String> {
fn into_response(self) -> Response<Body> {
let mut res = Response::new(Body::from(self.0));
*res.status_mut() = StatusCode::OK;
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res
}
}
}