Files
hyper/examples
rob2244 d894439e00 feat(service): change Service::call to take &self (#3223)
change Service::call to take &self instead of &mut self.
Because of this change, the trait bound in the service::util::service_fn and
the trait bound in the impl for the ServiceFn struct were changed from FnMut to Fn.
This change was decided on for the following reasons:
- It prepares the way for async fn,
  since then the future only borrows &self, and thus a Service can concurrently handle
  multiple outstanding requests at once.
- It's clearer that Services can likely be cloned
- To share state across clones you generally need Arc<Mutex<_>>
  that means you're not really using the &mut self and could do with a &self

Closes #3040

BREAKING CHANGE: The Service::call function no longer takes a mutable reference to self.
  The FnMut trait bound on the service::util::service_fn function and the trait bound
  on the impl for the ServiceFn struct were changed from FnMut to Fn.
2023-05-15 15:20:34 -04:00
..

Examples of using hyper

These examples show how to do common tasks using hyper. You may also find the Guides helpful.

If you checkout this repository, you can run any of the examples with the command:

cargo run --example {example_name} --features="full"

Dependencies

A complete list of dependencies used across these examples:

[dependencies]
hyper = { version = "1.0.0-rc.3", features = ["full"] }
tokio = { version = "1", features = ["full"] }
pretty_env_logger = "0.4"
http-body-util = "0.1.0-rc.2"
bytes = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
url = "2.2"
http = "0.2"
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }

Getting Started

Clients

  • client - A simple CLI http client that requests the url passed in parameters and outputs the response content and details to the stdout, reading content chunk-by-chunk.

  • client_json - A simple program that GETs some json, reads the body asynchronously, parses it with serde and outputs the result.

Servers

  • hello - A simple server that returns "Hello World!".

  • echo - An echo server that copies POST request's content to the response content.

Going Further

  • gateway - A server gateway (reverse proxy) that proxies to the hello service above.

  • http_proxy - A simple HTTP(S) proxy that handle and upgrade CONNECT requests and then proxy data between client and remote server.

  • multi_server - A server that listens to two different ports, a different Service per port.

  • params - A webserver that accept a form, with a name and a number, checks the parameters are presents and validates the input.

  • send_file - A server that sends back content of files using tokio-util to read the files asynchronously.

  • service_struct_impl - A struct that manually implements the Service trait and uses a shared counter across requests.

  • single_threaded - A server only running on 1 thread, so it can make use of !Send app state (like an Rc counter).

  • state - A webserver showing basic state sharing among requests. A counter is shared, incremented for every request, and every response is sent the last count.

  • upgrades - A server and client demonstrating how to do HTTP upgrades (such as WebSockets).

  • web_api - A server consisting in a service that returns incoming POST request's content in the response in uppercase and a service that calls the first service and includes the first service response in its own response.