as_box method and some fixes
This commit is contained in:
parent
fb9c4c97c5
commit
6e57913905
7 changed files with 24 additions and 6 deletions
|
@ -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<impl Sendable> {
|
||||
async fn on_request(&self, req: &HttpRequest) -> Option<Box<dyn Sendable>> {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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<impl Sendable> {
|
||||
async fn on_request(&self, req: &HttpRequest) -> Option<Box<dyn Sendable>> {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue