Integrating routing in the HTTP server
We are now at the stage of getting our HTTP server to accept incoming requests to create a range of tasks depending on what the URI is. To get our HTTP to support multiple tasks, we essentially must rewrite the handle
function in the src/main.rs
file. Before we rewrite the main
function, we must import what we need with the following code:
use hyper::body; use hyper::http::StatusCode;
We are importing these things because we are going to return a NOT_FOUND
status code if the wrong URI is passed. We also going to be extracting data from the body of the incoming request. Before we refactor our handle
function, we need to change our IncomingBody
struct to take in two integers taking the following form:
#[derive(Debug, Clone, Serialize, Deserialize)] pub struct IncomingBody { pub one: i32, pub two: i32 }
Inside our handle
function, we can define our Redis client, clean our URI...