Hyper is a versatile, fast, and safe HTTP library for Rust. It provides both client and server components, making it ideal for building web applications, APIs, and more. This article will introduce Hyper, explore its core features, and demonstrate how to use it to build a simple asynchronous HTTP server.
Hyper is built on top of the Tokio asynchronous runtime, which allows it to be highly concurrent and performant. It supports HTTP/1 and HTTP/2 out of the box and follows the zero-cost abstractions principle, ensuring that unused features do not affect your binary size or performance.
To start working with Hyper, add the following dependencies to your Cargo.toml
:
[dependencies]
hyper = "0.14"
tokio = { version = "1", features = ["full"] }
In this section, we will create a simple HTTP server that listens on port 8080 and responds with a “Hello, World!” message. To do this, follow these steps:
Add the necessary imports to your main.rs
file:
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use std::convert::Infallible;
use std::net::SocketAddr;
Create a request handler function that takes a Request
object and returns a Response
object wrapped in a Result
:
async fn handle_request(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let response = Response::builder()
.status(200)
.header("Content-Type", "text/plain")
.body(Body::from("Hello, World!"))
.unwrap();
Ok(response)
}
Create a server that binds to a socket address and uses the request handler function to process incoming requests:
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
let make_svc = make_service_fn(|_conn| {
let service = service_fn(handle_request);
async { Ok::<_, Infallible>(service) }
});
let server = Server::bind(&addr).serve(make_svc);
if let Err(e) = server.await {
eprintln!("Server error: {}", e);
}
}
To run the server, execute the following command in your terminal:
cargo run
Now, if you visit http://127.0.0.1:8080
in your browser or use a tool like curl
, you should see the “Hello, World!” message.
In this article, we have demonstrated how to use Hyper in Rust to create a simple asynchronous HTTP server. Although this example is minimal, Hyper provides a wide range of features and customization options that make it an excellent choice for building web applications, APIs, and more in Rust.
As you dive deeper into Hyper, you may want to explore its support for HTTP/2, streaming requests and responses, and other powerful features. The official Hyper documentation is a valuable resource to get started.