Fast and simple

New web framework alert: Meet Tower Web, the latest addition to Rust’s ecosystem

Eirini-Eleni Papadopoulou
© Shutterstock / SARIN KUNTHONG  

Time to introduce (yet another) web framework, this time for Rust! As we have argued before, there are never enough web frameworks so why not take a closer look at a new one? Meet Tower Web!

As we have argued before, there are never enough web frameworks around, so we are happy to introduce you to the latest addition in the universe of Rust web frameworks, Tower Web.

Tower Web is a fast and ergonomic web framework that aims to remove boilerplate and to decouple all HTTP concepts from the application logic.

As the official documentation states, “you implement your application using ‘plain old Rust types’ and Tower Web uses a macro to generate the necessary glue to serve the application as an HTTP service.”

Let’s check out the main features of this web framework to get a better idea of what it brings to the party.

impl_web! The impl_web! macro wraps one or more impl blocks and generates Resource implementations. These structs may then be passed to ServiceBuilder.
Routing – Attributes start with an HTTP verb and contain a path that is matched.

Captures – Path segments that begin with : are captures. They match any path segment and allow the resource method to get access to the value.

Method Arguments – impl_web! populates resource method arguments using data from the HTTP request.

Validation – The HTTP request can be validated by specifying an argument type that enforces an invariant.

SEE ALSO: Rust 2018 is in the works for a major release this year

Return type – Resource methods return types are futures yielding items that implement Response. This includes types like:String,serde_json::Value,http::Response#

Limitations – In order to work on stable Rust, impl_web! is implemented using proc-macro-hack, which comes with some limitations. The main one being that it can be used only once per scope.

derive(Extract)  Using derive(Extract) on a struct generates an Extract implementation, which enables the struct to be used as a resource method argument.

derive(Response) Using derive(Response) on a struct generates a Response implementation, which enables the struct to be used as a resource method return type.

Starting a server – Once Resource implementations are generated, the types may be passed to ServiceBuilder::resource in order to define the web service.

Testing – Because web services build with Tower Web are “plain old Rust types” (PORT?), testing a method is done the exact same way you would test any other rust code.

The modules featured in Tower Web include:

config   Application level configuration.
error   Error types and traits.
extract   Extract data from the HTTP request.
middleware   Middleware traits and implementations.
response   Types and traits for responding to HTTP requests.
routing   Map HTTP requests to Resource methods.
service   Define the web service as a set of routes, resources, middlewares, serializers, …
util   Utility types and traits.
view   Render content using templates

For more information on this web framework, please check out the official  API documentation or GitHub repo.

Getting started

As suggested in the GitHub repo, the best way to get started with Tower Web is to have a look at the examples and API docs.

Eirini-Eleni Papadopoulou
Eirini-Eleni Papadopoulou was the editor for Coming from an academic background in East Asian Studies, she decided that it was time to go back to her high-school hobby that was computer science and she dived into the development world. Other hobbies include esports and League of Legends, although she never managed to escape elo hell (yet), and she is a guest writer/analyst for competitive LoL at TGH.

Inline Feedbacks
View all comments