pub struct HttpRoutes<T> { /* private fields */ }
Expand description
An HTTP routes structure.
Implementations§
Source§impl<T: Send> HttpRoutes<T>
impl<T: Send> HttpRoutes<T>
Sourcepub fn add_route(
&mut self,
method: Method,
path: String,
handler: Box<dyn EndpointHandler<T> + Sync + Send>,
) -> Result<(), RouteError>
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.
Sourcepub fn handle_http_request(&self, request: &Request, argument: &T) -> Response
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more