Skip to content

Commit

Permalink
Update to hyper 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 18, 2023
1 parent d532f1c commit 7567423
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ repository = "https://github.com/dtolnay/rust-quiz"
[dependencies]
clap = { version = "4", features = ["deprecated", "derive"] }
futures = "0.3"
http = "0.2"
hyper = { version = "0.14", features = ["http1", "http2", "server", "tcp"] }
hyper-staticfile = "0.9"
http = "1"
hyper = { version = "1", features = ["http1", "http2", "server"] }
hyper-staticfile = "0.10"
hyper-util = { version = "0.1", features = ["tokio"] }
num_cpus = "1.0"
oqueue = "0.1"
parking_lot = "0.12"
Expand Down
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub enum Error {
#[error(transparent)]
Http(#[from] http::Error),

#[error(transparent)]
Hyper(#[from] hyper::Error),

#[error(transparent)]
Io(#[from] io::Error),

Expand Down
53 changes: 23 additions & 30 deletions src/serve.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use crate::Error;
use futures::future::{self, BoxFuture, Ready};
use futures::future::BoxFuture;
use http::response::Builder as ResponseBuilder;
use http::{header, StatusCode};
use hyper::server::conn::AddrStream;
use http::{header, Request, Response, StatusCode};
use hyper::body::Incoming;
use hyper::server::conn::http1;
use hyper::service::Service;
use hyper::{Body, Request, Response};
use hyper_staticfile::Static;
use hyper_staticfile::{Body, Static};
use hyper_util::rt::TokioIo;
use pin_project::pin_project;
use std::future::Future;
use std::io::{self, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::net::TcpListener;

const PORT: u16 = 8000;

Expand All @@ -31,7 +33,7 @@ impl Future for MainFuture {
let res = ResponseBuilder::new()
.status(StatusCode::MOVED_PERMANENTLY)
.header(header::LOCATION, "/rust-quiz/")
.body(Body::empty())
.body(Body::Empty)
.map_err(Error::Http);
Poll::Ready(res)
}
Expand All @@ -52,16 +54,12 @@ impl MainService {
}
}

impl Service<Request<Body>> for MainService {
impl Service<Request<Incoming>> for MainService {
type Response = Response<Body>;
type Error = Error;
type Future = MainFuture;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
fn call(&self, req: Request<Incoming>) -> Self::Future {
if req.uri().path() == "/" {
MainFuture::Root
} else {
Expand All @@ -70,31 +68,26 @@ impl Service<Request<Body>> for MainService {
}
}

struct MakeMainService;

impl Service<&AddrStream> for MakeMainService {
type Error = Error;
type Response = MainService;
type Future = Ready<Result<MainService, Error>>;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, _target: &AddrStream) -> Self::Future {
future::ok(MainService::new())
}
}

pub async fn main() -> Result<(), Error> {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), PORT);
let server = hyper::Server::try_bind(&addr)?.serve(MakeMainService);
let listener = TcpListener::bind(addr).await?;

let _ = writeln!(
io::stderr(),
"Quiz server running on http://localhost:{}/ ...",
PORT,
);

server.await.map_err(Error::Hyper)
loop {
let (tcp_stream, _socket_addr) = listener.accept().await?;
let io = TokioIo::new(tcp_stream);
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.serve_connection(io, MainService::new())
.await
{
let _ = writeln!(io::stderr(), "{}", err);
}
});
}
}

0 comments on commit 7567423

Please sign in to comment.