pub struct HttpRoutes<T> { /* private fields */ }
Expand description

An HTTP routes structure.

Implementations

Create a http request router.

Register a request handler for a unique (HTTP_METHOD, HTTP_PATH) tuple.

Arguments
  • method: HTTP method to assoicate with the handler.
  • path: HTTP path to associate with the handler.
  • handler: HTTP request handler for the (method, path) tuple.

Handle an incoming http request and generate corresponding response.

Examples
extern crate dbs_uhttp;
use dbs_uhttp::{
    EndpointHandler, HttpRoutes, Method, StatusCode, Request, Response, Version
};

struct HandlerArg(bool);
struct MockHandler {}
impl EndpointHandler<HandlerArg> for MockHandler {
    fn handle_request(&self, _req: &Request, _arg: &HandlerArg) -> Response {
        Response::new(Version::Http11, StatusCode::OK)
    }
}

let mut router = HttpRoutes::new("Mock_Server".to_string(), "/api/v1".to_string());
let handler = MockHandler {};
router.add_route(Method::Get, "/func1".to_string(), Box::new(handler)).unwrap();

let request_bytes = b"GET http://localhost/api/v1/func1 HTTP/1.1\r\n\r\n";
let request = Request::try_from(request_bytes, None).unwrap();
let arg = HandlerArg(true);
let reply = router.handle_http_request(&request, &arg);
assert_eq!(reply.status(), StatusCode::OK);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.