Skip to content

Latest commit

 

History

History

examples

tower-web usage examples.

This directory contains a number of examples showcasing various capabilities of tower-web.

All examples can be executed with:

cargo run --example $name

It is recommended to explore the examples in (approximately) the following order:

  • hello_world - getting started with tower_web. This demonstrates how to get a basic web service running.

  • args - Handler arguments are populated using the HTTP request.

  • derive_extract - Custom type handler arguments are populated using the HTTP request.

  • json - Receiving and responding with JSON. This example also shows how to customize the HTTP response status and headers.

  • static_file - Respond with static files from disk. This examplee also shows glob path parameteres.

  • middleware - Decorate the application with middleware. Doing so adds additional functionality. This example adds request logging.

  • html_handlebars - Respond with HTML by rendering handlebars templates.

Tower Web provides experimental support for Rust's async / await syntax. To use this syntax, the Rust nightly release channel is required and the crate must be set to the 2018 edition.

The example in the async-await directory contains a Cargo.toml as well as code.

  1. Add edition = 2018 to your Cargo.toml.
  2. Add features = ["async-await-preview"] to the tower-web dependency.
  3. Use the necessary nightly features in the application.
  4. Import Tokio's await! macro. *
  5. Define async handlers.

* You should use nightly version prior to nightly-2019-05-09. As of that version the syntax change for .await syntax and tokio 0.1 is probably not going to track nightly changes.

Support for serving data over TLS is provided with the rustls feature. The rustls directory contains an example along with a Cargo.toml file.

  1. Add features = ["rustls"] to the tower-web dependency.
  2. Import tokio-rustls.
  3. Configure TLSAcceptor.
  4. Wrap incoming TcpStream and handle errors