Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
41 changes: 40 additions & 1 deletion docs/content/web-frameworks.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Web framework integration

Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [Tide], [Axum], [Poem], and [Salvo].
Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [Tide], [Axum], [Poem], [Salvo], and [Ntex].

[Actix]: https://actix.rs/
[Rocket]: https://rocket.rs/
Expand All @@ -11,6 +11,7 @@ Maud includes support for these web frameworks: [Actix], [Rocket], [Rouille], [T
[Submillisecond]: https://github.com/lunatic-solutions/submillisecond
[Poem]: https://github.com/poem-web/poem
[Salvo]: https://salvo.rs
[Ntex]: https://ntex.rs/

# Actix

Expand Down Expand Up @@ -304,3 +305,41 @@ async fn main() {
Server::new(listener).serve(app).await;
}
```

# Ntex

ntex support is available with the "ntex" feature:

```toml
# ...
[dependencies]
maud = { version = "*", features = ["ntex"] }
# ...
```

ntex request handlers can use a `Markup` that implements the `ntex::web::Responder` trait.

```rust,no_run
use ntex::web::{get, App, HttpServer, Responder};
use maud::{html, Markup};
use std::io;

#[get("/")]
async fn index() -> impl Responder {
html! {
html {
body {
h1 { "Hello World!" }
}
}
}
}

#[ntex::main]
async fn main() -> io::Result<()> {
HttpServer::new(|| App::new().service(index))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
```
3 changes: 2 additions & 1 deletion doctest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
actix-web = { version = "4.0.0-rc.2", default-features = false, features = ["macros"] }
ammonia = "3"
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp", "submillisecond", "poem", "salvo"] }
maud = { path = "../maud", features = ["actix-web", "rocket", "tide", "axum", "warp", "submillisecond", "poem", "salvo", "ntex"] }
pulldown-cmark = "0.8"
rocket = "0.5"
rouille = "3"
Expand All @@ -18,6 +18,7 @@ axum = "0.8"
warp = "0.3.6"
poem = "3"
salvo = "0.78.0"
ntex = { version = "2" }

[dependencies.async-std]
version = "1.9.0"
Expand Down
2 changes: 2 additions & 0 deletions maud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ default = []
actix-web = ["actix-web-dep", "futures-util"]
axum = ["axum-core", "http"]
salvo = ["salvo_core", "http"]
ntex = ["dep:ntex"]

[dependencies]
maud_macros = { version = "0.27.0", path = "../maud_macros" }
Expand All @@ -35,6 +36,7 @@ http = { version = "1", optional = true }
warp = { version = "0.3.6", optional = true }
poem = { version = "3", optional = true }
salvo_core = { version = "0.78.0", optional = true }
ntex = { version = "2.0", optional = true, default-features = false }

[dev-dependencies]
trybuild = { version = "1.0.33", features = ["diff"] }
Expand Down
45 changes: 45 additions & 0 deletions maud/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,51 @@ mod actix_support {
}
}

#[cfg(feature = "ntex")]
mod ntex_support {
use core::{
error::Error,
mem,
task::{Context, Poll},
};
use ntex::{
http::{
body::{BodySize, MessageBody},
header, StatusCode,
},
util::Bytes,
web::{ErrorRenderer, HttpRequest, HttpResponse, Responder},
};

use crate::PreEscaped;
use alloc::{rc::Rc, string::String};

impl MessageBody for PreEscaped<String> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is implementing MessageBody necessary here, given that we implement Responder directly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MessageBody adds flexibility, allowing maud to be used anywhere the body it's accepted, not only in the Responder.

fn size(&self) -> BodySize {
self.0.size()
}

fn poll_next_chunk(
&mut self,
_: &mut Context<'_>,
) -> Poll<Option<Result<Bytes, Rc<dyn Error>>>> {
if self.0.is_empty() {
Poll::Ready(None)
} else {
Poll::Ready(Some(Ok(Bytes::from(mem::take(&mut self.0).into_bytes()))))
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we forward to the implementation for String?

Suggested change
if self.0.is_empty() {
Poll::Ready(None)
} else {
Poll::Ready(Some(Ok(Bytes::from(mem::take(&mut self.0).into_bytes()))))
}
self.0.poll_next_chunk(context)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation follows ntex impl MessageBody for String, avoiding unnecessary allocations and one-shot consumption of the body, performance wise it's the best.

}
}

impl<Err: ErrorRenderer> Responder<Err> for PreEscaped<String> {
async fn respond_to(self, _: &HttpRequest) -> HttpResponse {
HttpResponse::build(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(self.0)
}
}
}

#[cfg(feature = "tide")]
mod tide_support {
use crate::PreEscaped;
Expand Down