diff --git a/examples/simple_site.rs b/examples/simple_site.rs index faa7af7..da052f5 100755 --- a/examples/simple_site.rs +++ b/examples/simple_site.rs @@ -1,5 +1,6 @@ use std::time::Duration; +use async_trait::async_trait; use ezhttp::{ body::Body, headers::Headers, @@ -62,14 +63,15 @@ impl EzSite { } } +#[async_trait] impl HttpServer for EzSite { - async fn on_request(&self, req: &HttpRequest) -> Option { + async fn on_request(&self, req: &HttpRequest) -> Option> { println!("{} > {} {}", req.addr, req.method, req.url.to_path_string()); if let Some(resp) = self.get_main_page(req).await { - Some(resp) + Some(resp.as_box()) } else if let Some(resp) = self.get_unknown_page(req).await { - Some(resp) + Some(resp.as_box()) } else { None // shutdown connection } diff --git a/examples/small_site.rs b/examples/small_site.rs index e9b2793..cb53108 100755 --- a/examples/small_site.rs +++ b/examples/small_site.rs @@ -1,9 +1,11 @@ +use async_trait::async_trait; use ezhttp::{prelude::*, Sendable}; struct EzSite(String); +#[async_trait] impl HttpServer for EzSite { - async fn on_request(&self, req: &HttpRequest) -> Option { + async fn on_request(&self, req: &HttpRequest) -> Option> { println!("{} > {} {}", req.addr, req.method, req.url.to_path_string()); if req.url.path == "/" { @@ -13,7 +15,7 @@ impl HttpServer for EzSite { ("Content-Type", "text/html"), // - content type ("Content-Length", self.0.len().to_string().as_str()) // - content length ]), Body::from_text(&self.0.clone()), // response body - )) + ).as_box()) } else { None // close connection } diff --git a/src/ezhttp/body.rs b/src/ezhttp/body.rs index 74ce284..9b11a07 100755 --- a/src/ezhttp/body.rs +++ b/src/ezhttp/body.rs @@ -160,6 +160,9 @@ impl Sendable for Body { ) -> Result<(), HttpError> { stream.write_all(&self.as_bytes()).await.map_err(|_| HttpError::WriteHeadError) } + fn as_box(self) -> Box { + Box::new(self) + } } impl Default for Body { diff --git a/src/ezhttp/headers.rs b/src/ezhttp/headers.rs index 397b161..531948b 100755 --- a/src/ezhttp/headers.rs +++ b/src/ezhttp/headers.rs @@ -144,4 +144,7 @@ impl Sendable for Headers { } stream.write_all(head.as_bytes()).await.map_err(|_| HttpError::WriteHeadError) } + fn as_box(self) -> Box { + Box::new(self) + } } \ No newline at end of file diff --git a/src/ezhttp/mod.rs b/src/ezhttp/mod.rs index 2ab4401..a325890 100755 --- a/src/ezhttp/mod.rs +++ b/src/ezhttp/mod.rs @@ -16,7 +16,8 @@ pub mod prelude { pub use super::server::*; pub use super::server::handler::*; pub use super::server::starter::*; - pub use super::client::*; + // pub use super::client::*; + pub use super::*; } use error::HttpError; @@ -113,6 +114,7 @@ pub trait Sendable: Send + Sync { &self, stream: &mut (dyn AsyncWrite + Unpin + Send + Sync), ) -> Result<(), HttpError>; + fn as_box(self) -> Box; } pub type Stream = TimeoutStream; \ No newline at end of file diff --git a/src/ezhttp/request.rs b/src/ezhttp/request.rs index 1047a20..ddd3928 100755 --- a/src/ezhttp/request.rs +++ b/src/ezhttp/request.rs @@ -216,4 +216,7 @@ impl Sendable for HttpRequest { Ok(()) } + fn as_box(self) -> Box { + Box::new(self) + } } \ No newline at end of file diff --git a/src/ezhttp/response.rs b/src/ezhttp/response.rs index 0c35915..8566a64 100755 --- a/src/ezhttp/response.rs +++ b/src/ezhttp/response.rs @@ -93,4 +93,7 @@ impl Sendable for HttpResponse { Ok(()) } + fn as_box(self) -> Box { + Box::new(self) + } }