Struct HttpHandler

Source
pub struct HttpHandler<H: HttpService>(/* private fields */);
Available on crate feature http only.
Expand description

Adapter to use types from the http crate.

The inner request handler must implement the HttpService trait.

Implementations§

Source§

impl<H: HttpService> HttpHandler<H>

Source

pub fn new(unit_service: H) -> Self

Examples found in repository?
examples/http_adapter.rs (lines 10-28)
7fn main() {
8    let mut unit = Unit::new().unwrap();
9
10    unit.set_request_handler(HttpHandler::new(|req: Request<Vec<u8>>| {
11        let body = format!("Hello world!\n\nBody length: {}\n", req.body().len());
12
13        // The Unit server redirects stdout/stderr to the server log.
14        eprintln!("Received reqest for: {}", req.uri().path());
15
16        if req.uri().path() == "/panic" {
17            // The HttpHandler converts panics into 501 errors. This imposes an
18            // UnwindSafe restriction on the request handler.
19            panic!("The /panic path panics!")
20        }
21
22        let response = Response::builder()
23            .header("Content-Type", "text/plain")
24            .body(body.into_bytes())
25            .unwrap();
26
27        Ok(response)
28    }));
29
30    unit.run();
31}

Trait Implementations§

Auto Trait Implementations§

§

impl<H> Freeze for HttpHandler<H>
where H: Freeze,

§

impl<H> RefUnwindSafe for HttpHandler<H>
where H: RefUnwindSafe,

§

impl<H> Send for HttpHandler<H>
where H: Send,

§

impl<H> Sync for HttpHandler<H>
where H: Sync,

§

impl<H> Unpin for HttpHandler<H>
where H: Unpin,

§

impl<H> UnwindSafe for HttpHandler<H>

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.