Adding the server instance to a runtime

Since we now have a handler, we can start the server. The runtime expects a Future instance with the Future<Item = (), Error = ()> type, but the Server struct implements a Future with the hyper::Error error type. We can use this error to inform the user about issues, but in our example we'll just drop any error. As you might remember, the drop function expects a single argument of any type and returns a unit empty type. The Future trait uses the map_err method. It changes the error type using a function, which expects the original error type and returns a new one. Drop an error from the server using the following:

let server = server.map_err(drop);

We now have everything we need and can start the server with the specific runtime. Use the hyper::rt::run function to start the server:

hyper::rt::run(server);

Don't compile it yet, because we haven't imported types. Add it to the head of a source file:

use hyper::{Body, Response, Server};
use hyper::rt::Future;
use hyper::service::service_fn_ok;

We need to import the different hyper types that we are using: Server, Response, and Body. In the final line, we're using the service_fn_ok function. The Future import needs special attention; it's the re-exported trait of the futures crate and it's used everywhere in the hyper crate. In the next chapter, we'll examine this trait in detail.

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

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