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 withtower_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.
- Add
edition = 2018
to yourCargo.toml
. - Add
features = ["async-await-preview"]
to thetower-web
dependency. - Use the necessary nightly features in the application.
- Import Tokio's
await!
macro. * - 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.
- Add
features = ["rustls"]
to thetower-web
dependency. - Import tokio-rustls.
- Configure TLSAcceptor.
- Wrap incoming TcpStream and handle errors