Struct Handler

Source
pub struct Handler { /* private fields */ }
Expand description

Handler

Matches requests by their method and url, and executes different handler functions for them.

§Example

use http_srv::handler::{self,*};
use http::request::HttpRequest;
use http::HttpMethod;

let mut handler = Handler::new();
handler.get("/", |req: &mut HttpRequest| {
    req.respond_str("Hello world! :)")
});
handler.add_default(HttpMethod::GET, handler::cat_handler);
handler.post_interceptor(handler::log_stdout);

Implementations§

Source§

impl Handler

Source

pub fn new() -> Self

Source

pub fn get(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)

Shortcut for add(HttpMethod::GET, …)

Source

pub fn post(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)

Shortcut for add(HttpMethod::POST, …)

Source

pub fn delete(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)

Shortcut for add(HttpMethod::DELETE, …)

Source

pub fn head(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)

Shortcut for add(HttpMethod::HEAD, …)

Source

pub fn add( &mut self, method: HttpMethod, url: impl Into<UrlMatcher>, f: impl RequestHandler, )

Adds a handler for a request type

  • method: HTTP method to match
  • url: URL for the handler
  • f: Handler for the request
Source

pub fn add_default(&mut self, method: HttpMethod, f: impl RequestHandler)

Adds a default handler for all requests of a certain type

Source

pub fn pre_interceptor(&mut self, f: impl Interceptor)

Add a function to run before the request is processed

Source

pub fn post_interceptor(&mut self, f: impl Interceptor)

Add a function to run after the request is processed

Source

pub fn get_handler( &self, method: &HttpMethod, url: &str, ) -> Option<&dyn RequestHandler>

Get the handler for a certain method and url

Source

pub fn handle(&self, req: &mut HttpRequest) -> Result<()>

Handles a request if it finds a RequestHandler for it. Else, it returns a 403 FORBIDDEN response

Trait Implementations§

Source§

impl Default for Handler

Source§

fn default() -> Self

Default Handler

§Pre Interceptors
§Handler Functions
§Post Interceptors

Auto Trait Implementations§

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.