Working with Hyper library for http server


#1

Hi everyone.
I’m making my way through creating http server using hyper lib and here is what i encountered with:
The Documentation gives the information about hyper::server::Response type. It says that there is send() method to create body and send the response to a request; But there is no such method for it when i code it. Is there any alternate way???


#2

A hyper server is done by implementing its NewService trait: https://docs.rs/hyper/0.11.12/hyper/server/trait.NewService.html

NewService is essentially a factory that creates a fresh implementation (per client connection) of something that implements the Service trait: https://docs.rs/hyper/0.11.12/hyper/server/trait.Service.html

The Service impl you provide is the core of your server - that’s where you receive a request and then return some Future impl that contains the response. A simple example is https://docs.rs/hyper/0.11.12/hyper/server/trait.Service.html#server. This example also shows how you set the body of the response.

Once you’ve defined impls for the above, you bootstrap the server with https://docs.rs/hyper/0.11.12/hyper/server/struct.Http.html#method.bind.


#3

That was quite a piece of information Sir. Looks like things are getting more complicated. I was going through the Routing with hyper server, and i cant even emagine how to send a response to a routing request. I can only guess: I have to declare a handler for routing with .using(fn_name)… where i will create an instance that impls a Service trait and to call the call(rec:http::Request) that will send an immediate future Response? It will be undoubtfully great to get some coding example of a routing handler for hyper server to Respond on a Request.


#4

Here’s a small example of a server doing routing and different response styles (including holding the connection and pushing data down the stream).

It’s sadly undocumented, I wrote it for a training.


#5

If you need more than basic routing (and other framework features common to a web server), you may want to look at higher level libs/frameworks than hyper. For example:

  1. iron + its iron router crate
  2. gotham
  3. rocket (note that rocket requires the nightly compiler)

#1 and #2 build on top of hyper. There may be others, but the above are ones I’m aware of offhand.


#6

If you need more than routing (websockets, http/2) , Another high level web framework to consider is actix web