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>
impl<H: HttpService> HttpHandler<H>
Sourcepub fn new(unit_service: H) -> Self
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§
Source§impl<H: HttpService + RefUnwindSafe> UnitService for HttpHandler<H>
impl<H: HttpService + RefUnwindSafe> UnitService for HttpHandler<H>
fn handle_request(&mut self, req: Request<'_>) -> UnitResult<()>
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> 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