Building the HTTP server
For our HTTP server, we need to carry out the following steps:
- Define a struct that deserializes the HTTP request body.
- Define a function that handles the incoming request.
- Define pathways for the program to run based on environment variables.
- Run a server that listens for incoming requests.
We are not going to section off individual sections for each step as we have covered all of these steps/processes in the previous chapter. Before we carry out all the steps, we must import the following into the src/main.rs file:
use hyper::{Body, Request, Response, Server};
use hyper::body;
use hyper::service::{make_service_fn, service_fn};
use std::net::SocketAddr;
use std::env;
use serde::{Serialize, Deserialize};
use serde_json;
use bytes::{BufMut, BytesMut};
You should be familiar with all these imports apart from the bytes import, which we will cover when defining the HTTP handle function. First, we will define a trivial struct...