Tower Web 0.3 — async/await and template support

Happy Friday!

Today, Tower Web 0.3 has been released and it comes with two major new features:

  • Experimental support for async/await.
  • Support for using templates to render responses.

Async/await support

In case you haven’t heard, / is a big new feature that is being worked on for Rust. It aims to make asynchronous programming easy (well, at least a little bit easier than it is today). The work has been on going for a while and is already usable today on the Rust nightly channel.

With Tower Web 0.3, you can easily implement handlers using this new syntax.

In this example, is an asynchronous operation. The macro waits (asynchronously) for the result of before continuing to process the response. This enables asynchronous implementations that feel like writing blocking code.

Tower Web’s / support is based on Tokio’s. To get started, you will need to be on a recent Rust nightly release. In your file, you will need to opt into Rust 2018 edition as well as Tower Web’s feature flag.

You will also need to enable some unstable features:

Implementation wise, there is not much of a difference between how Tower Web handles functions compared to functions returning . The response future ends up being boxed into a trait object.

For a slightly more complex example, lets look at defining a response handler that issues an HTTP request of its own.

You find see the full example here.

Template support

Templates may now be used to render responses. This is done by returning a “plain old Rust struct” as the return value and using an attribute to define the template to use to render the response. The template engine will render the template, populating variables using the data returned from the response handler.

This work paves the way to support custom response serializer. A response serializer handles a specific content type and is able to take a “plain old Rust types”, converting it into a response of that content type.

Let’s look at how it works in practice.

First, the template engine has to be added to the service. Currently, only the Handlebars template engine is supported, but this will change over time.

By default, the serializer expectes a directory at the root of your crate. Templates are added to this directory. For example, is added containing the following:

The template engine replaces with the value of the field from the response handler’s return value.

Now the response handler is defined:

contains a field named . This will be used to populate the HTML title in the template. The response handler is annotated with
and . The annotation informs Tower Web that the response should be serialized as HTML. The set of available serializers is searched for one that handles this content type. The added serializer handles the HTML content type and is used to serialize . The serializer looks for the template attribute to pick which template is used to serialize .

Ideally, neither of these annotations would be required. Eventually, the content type will be inferred using the request’s header and the template will be picked using a convention. These are features for future releases.

You find see the full example here.

More to come

Work on Tower Web will continue. It is ground for experimenting with Web APIs for Rust. It is allowing for abstractions like , , serializers (discussed above), and more to be fleshed out. These abstractions are being extracted as they are ready to higher level crates.

As mentioned previously, the plan is still to merge the ideas developed in Tower Web with Warp. This is taking longer than initially expected, but mostly because I hope to get to the point where the Tower Web abstractions are somewhat stable before merging.

As always, please try out Tower Web and provide feedback. The Tower Gitter channel is open for discussion.

I do stuff. I say stuff. It's serious business.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store