3 Create your first route handler

 

This chapter covers

  • Adding a route handler to your server
  • Understanding Warp’s Filter trait
  • Serializing data to JSON
  • Returning a proper HTTP response
  • Sending back various HTTP error codes
  • Setting up the handling of CORS

In the first part of this chapter, we will set up a basic web service with all the needed tooling to serve as the baseline for our journey throughout the book. The second part will show how easy it is to implement the handling of cross-origin resource sharing (CORS) in our web server; browsers can reach us even if they are not on the same domain.

This chapter lays the groundwork for working with Warp and sets up our web server, which we will build on in the upcoming chapters. It will teach you how Warp is processing HTTP requests (through its filter system), which later chapters will use to add middleware and pass around state.

From now on, we have to be a bit more opinionated and, as mentioned in chapter 2, we are choosing Warp as our web framework. All the code we are going to talk about can be found in the GitHub repository for this book (https://github.com/Rust-Web-Development/code).

Look at figure 3.1 to remind yourself about the tech stack included in a framework. You will always also choose a runtime and a library that abstracts over the HTTP server. The HTTP library Hyper is already included in Warp, whereas Tokio has to be added to our Cargo.toml file.

3.1 Getting to know our web framework: Warp

 
 
 

3.1.1 What is included in Warp

 
 
 

3.1.2 Warp’s filter system

 
 
 

3.2 GET your first JSON response

 

3.2.1 Align with your framework’s way of thinking

 
 
 

3.2.2 Handle the success route

 
 
 
 

3.2.3 Get help from Serde

 
 
 

3.2.4 Handle errors gracefully

 

3.3 Handling CORS headers

 
 
 

3.3.1 Returning CORS headers on the application level

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest