Add axum support ()

Co-authored-by: Chris Wong <lambda.fairy@gmail.com>
This commit is contained in:
Marcel Müller 2021-08-17 09:25:07 +00:00 committed by GitHub
parent 9a2c4fba4f
commit cc62bcbb86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 1 deletions

View file

@ -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

View file

@ -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();
}
```

View file

@ -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"

View file

@ -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"] }

View file

@ -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
}
}
}