diff --git a/crates/http/src/handler/mod.rs b/crates/http/src/handler/mod.rs index 41ad18f..b318e6e 100644 --- a/crates/http/src/handler/mod.rs +++ b/crates/http/src/handler/mod.rs @@ -37,8 +37,8 @@ use std::future::Future; /// * `RespBody`: The response body type that implements [`Body`] /// * `Error`: The error type that can be converted into a boxed error /// * `Fut`: The future type returned by the handler -#[trait_variant::make(Handler: Send)] -pub trait LocalHandler: Sync { +#[trait_variant::make(Send)] +pub trait Handler: Sync { /// The type of the response body type RespBody: Body; diff --git a/crates/web/examples/return_uid.rs b/crates/web/examples/return_uid.rs index 98dbdc5..8512132 100644 --- a/crates/web/examples/return_uid.rs +++ b/crates/web/examples/return_uid.rs @@ -1,7 +1,7 @@ -use http::StatusCode; use micro_web::date::DateServiceDecorator; use micro_web::router::{get, post, Router}; -use micro_web::{handler_fn, PathParams, Server}; +use micro_web::{handler_fn, responder, PathParams, Server}; +use micro_web::responder::Responder; async fn empty_body() -> &'static str { "" @@ -11,8 +11,8 @@ async fn echo_uid<'s, 'r>(path_params: &PathParams<'s, 'r>) -> String { path_params.get("id").map(|s| s.to_owned()).unwrap() } -async fn default_handler() -> (&'static str, StatusCode) { - ("404 not found", StatusCode::NOT_FOUND) +async fn default_handler() -> impl Responder { + responder::NotFound } #[tokio::main] diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rs index f9ca72d..1729af6 100644 --- a/crates/web/src/lib.rs +++ b/crates/web/src/lib.rs @@ -102,10 +102,10 @@ mod body; mod fn_trait; mod handler; mod request; -mod responder; mod server; // Public modules +pub mod responder; pub mod date; pub mod decorator; pub mod encoding; diff --git a/crates/web/src/responder.rs b/crates/web/src/responder.rs index 9727c23..910a0a1 100644 --- a/crates/web/src/responder.rs +++ b/crates/web/src/responder.rs @@ -115,3 +115,12 @@ impl Responder for Infallible { unreachable!() } } + +pub struct NotFound; + +impl Responder for NotFound { + #[inline] + fn response_to(self, req: &RequestContext) -> Response { + ("404 Not Found.", StatusCode::NOT_FOUND).response_to(req) + } +}