#![deny(warnings)]
#![allow(unused_imports)]
use http_body_util::Full;
use hyper::body::Bytes;
#[cfg(feature = "server")]
use hyper::server::conn::http2;
use hyper::service::service_fn;
use hyper::{Request, Response};
use std::convert::Infallible;
use std::net::SocketAddr;
use tokio::net::TcpListener;
#[path = "../benches/support/mod.rs"]
mod support;
use support::TokioIo;
#[cfg(feature = "server")]
async fn hello(_: Request<hyper::body::Incoming>) -> Result<Response<Full<Bytes>>, Infallible> {
Ok(Response::new(Full::new(Bytes::from("Hello, World!"))))
}
#[derive(Clone)]
pub struct TokioExecutor;
impl<F> hyper::rt::Executor<F> for TokioExecutor
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
fn execute(&self, fut: F) {
tokio::task::spawn(fut);
}
}
#[cfg(feature = "server")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
pretty_env_logger::init();
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = TcpListener::bind(addr).await?;
loop {
let (stream, _) = listener.accept().await?;
let io = TokioIo::new(stream);
tokio::task::spawn(async move {
if let Err(err) = http2::Builder::new(TokioExecutor)
.serve_connection(io, service_fn(hello))
.await
{
eprintln!("Error serving connection: {}", err);
}
});
}
}
#[cfg(not(feature = "server"))]
fn main() {
panic!("This example requires the 'server' feature to be enabled");
}