Setting the requests handler

The Builder struct provides methods to tweak the parameters of the server created. For example, hyper's server supports both HTTP1 and HTTP2. You can use a builder value to choose either one protocol or both. In the following example, we're using builder to attach a service for handling incoming HTTP requests using the serve method:

let server = builder.serve(|| {
service_fn_ok(|_| {
Response::new(Body::from("Almost microservice..."))
})
});

Here, we're using the builder instance to attach a function that generates a Service instance. This function implements the hyper::service::NewService trait. The generated item then has to implement the hyper::service::Service trait. A service in a hyper crate is a function that takes a request and gives a response back. We haven't implemented this trait in this example; instead, we'll use the service_fn_ok function, which turns a function with suitable types into a service handler.

There are two corresponding structs: hyper::Request and hyper::Response. In the preceding code, we ignored a request argument and constructed the same response for every request. The response contains a body of static text.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.59