Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Created November 17, 2025 03:40
Show Gist options
  • Select an option

  • Save masakielastic/3ac56d01db0a0ab3e207c62ffa001c1d to your computer and use it in GitHub Desktop.

Select an option

Save masakielastic/3ac56d01db0a0ab3e207c62ffa001c1d to your computer and use it in GitHub Desktop.
hyper で HTTP/1 サーバー

hyper で HTTP/1 サーバー

構成

  • Cargo.toml
  • src/main.rs

実行

cargo run

サーバーが起動したら curl で確認します。

curl -v http://localhost:3000
[package]
name = "hyper-minimal"
version = "0.1.0"
edition = "2024"
[dependencies]
hyper = { version = "1.5", features = ["server", "http1"] }
hyper-util = { version = "0.1", features = ["tokio", "server", "http1"] }
tokio = { version = "1", features = ["full"] }
use hyper::{Request, Response, body::Incoming};
use hyper::service::service_fn;
use hyper_util::rt::TokioIo;
use hyper_util::server::conn::auto::Builder;
use tokio::net::TcpListener;
use std::convert::Infallible;
async fn handle_request(_req: Request<Incoming>) -> Result<Response<String>, Infallible> {
Ok(Response::new("Hello, World!".to_string()))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "127.0.0.1:3000";
let listener = TcpListener::bind(addr).await?;
println!("Listening on http://{}", addr);
loop {
let (stream, _) = listener.accept().await?;
let io = TokioIo::new(stream);
tokio::spawn(async move {
if let Err(err) = Builder::new(hyper_util::rt::TokioExecutor::new())
.serve_connection(io, service_fn(handle_request))
.await
{
eprintln!("Error serving connection: {:?}", err);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment