hyper_router/
handlers.rs

1use hyper::header::{CONTENT_LENGTH, CONTENT_TYPE};
2use hyper::{Body, Request, Response, StatusCode};
3
4pub fn default_404_handler(_: Request<Body>) -> Response<Body> {
5    let body = "page not found";
6    make_response(&body, StatusCode::NOT_FOUND)
7}
8
9pub fn method_not_supported_handler(_: Request<Body>) -> Response<Body> {
10    let body = "method not supported";
11    make_response(&body, StatusCode::METHOD_NOT_ALLOWED)
12}
13
14pub fn internal_server_error_handler(_: Request<Body>) -> Response<Body> {
15    let body = "internal server error";
16    make_response(&body, StatusCode::INTERNAL_SERVER_ERROR)
17}
18
19pub fn not_implemented_handler(_: Request<Body>) -> Response<Body> {
20    let body = "not implemented";
21    make_response(&body, StatusCode::NOT_IMPLEMENTED)
22}
23
24fn make_response(body: &'static str, status: StatusCode) -> Response<Body> {
25    Response::builder()
26        .status(status)
27        .header(CONTENT_LENGTH, body.len() as u64)
28        .header(CONTENT_TYPE, "text/plain")
29        .body(Body::from(body))
30        .expect("Failed to construct response")
31}