Struct HttpRoutes

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

An HTTP routes structure.

Implementations§

Source§

impl<T: Send> HttpRoutes<T>

Source

pub fn new(server_id: String, prefix: String) -> Self

Create a http request router.

Source

pub fn add_route( &mut self, method: Method, path: String, handler: Box<dyn EndpointHandler<T> + Sync + Send>, ) -> Result<(), RouteError>

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.
Source

pub fn handle_http_request(&self, request: &Request, argument: &T) -> Response

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§

§

impl<T> Freeze for HttpRoutes<T>

§

impl<T> !RefUnwindSafe for HttpRoutes<T>

§

impl<T> Send for HttpRoutes<T>

§

impl<T> Sync for HttpRoutes<T>

§

impl<T> Unpin for HttpRoutes<T>

§

impl<T> !UnwindSafe for HttpRoutes<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.