New web framework alert: Meet Tower Web, the latest addition to Rust’s ecosystem
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
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.
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