-
Notifications
You must be signed in to change notification settings - Fork 10
/
http-stream.rs
46 lines (40 loc) · 1.34 KB
/
http-stream.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use futures_util::stream::StreamExt;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{Request, Response};
use hyper_util::rt::tokio::TokioIo;
use std::convert::Infallible;
use std::future::ready;
use tokio::net::TcpListener;
use tls_listener::TlsListener;
mod tls_config;
use tls_config::tls_acceptor;
async fn hello(_: Request<impl hyper::body::Body>) -> Result<Response<String>, Infallible> {
Ok(Response::new("Hello, World!".into()))
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3000).into();
// This uses a filter to handle errors with connecting
TlsListener::new(tls_acceptor(), TcpListener::bind(addr).await?)
.connections()
.filter_map(|conn| {
ready(match conn {
Err(err) => {
eprintln!("Error: {:?}", err);
None
}
Ok(c) => Some(TokioIo::new(c)),
})
})
.for_each_concurrent(None, |conn| async {
if let Err(err) = http1::Builder::new()
.serve_connection(conn, service_fn(hello))
.await
{
eprintln!("Error serving connection: {:?}", err);
}
})
.await;
Ok(())
}