Skip to content

Commit

Permalink
feat(responder): add NotFound responder
Browse files Browse the repository at this point in the history
  • Loading branch information
zavakid committed Feb 9, 2025
1 parent 5576dec commit b82e967
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/http/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions crates/web/examples/return_uid.rs
Original file line number Diff line number Diff line change
@@ -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 {
""
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions crates/web/src/responder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ impl Responder for Infallible {
unreachable!()
}
}

pub struct NotFound;

impl Responder for NotFound {
#[inline]
fn response_to(self, req: &RequestContext) -> Response<ResponseBody> {
("404 Not Found.", StatusCode::NOT_FOUND).response_to(req)
}
}

0 comments on commit b82e967

Please sign in to comment.