aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs67
1 files changed, 31 insertions, 36 deletions
diff --git a/src/main.rs b/src/main.rs
index aa13609..5230434 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@
pub mod config;
pub mod error;
pub mod files;
+pub mod helper;
pub mod limiter;
pub mod proxy;
@@ -15,6 +16,7 @@ use crate::{
use anyhow::{anyhow, bail, Context, Result};
use error::ServiceError;
use futures::future::try_join_all;
+use helper::TokioIo;
use http_body_util::{combinators::BoxBody, BodyExt};
use hyper::{
body::Incoming,
@@ -27,11 +29,7 @@ use hyper::{
use limiter::Limiter;
use log::{debug, error, info, warn};
use std::{fs::File, io::BufReader, net::SocketAddr, path::Path, process::exit, sync::Arc};
-use tokio::{
- io::{AsyncRead, AsyncWrite},
- net::TcpListener,
- signal::ctrl_c,
-};
+use tokio::{net::TcpListener, signal::ctrl_c};
use tokio_rustls::TlsAcceptor;
pub struct State {
@@ -90,18 +88,17 @@ async fn serve_http(state: Arc<State>) -> Result<()> {
None => return Ok(()),
};
- let listen_futs: Result<Vec<()>> = try_join_all(http_config.bind
- .iter()
- .map(|e| async {
- let l = TcpListener::bind(e.clone()).await?;
- loop {
- let (stream, addr) = l.accept().await.context("accepting connection")?;
- debug!("connection from {addr}");
- let config = state.clone();
- tokio::spawn(async move { serve_stream(config, stream, addr).await });
- }
- }))
- .await;
+ let listen_futs: Result<Vec<()>> = try_join_all(http_config.bind.iter().map(|e| async {
+ let l = TcpListener::bind(e.clone()).await?;
+ loop {
+ let (stream, addr) = l.accept().await.context("accepting connection")?;
+ debug!("connection from {addr}");
+ let stream = TokioIo(stream);
+ let config = state.clone();
+ tokio::spawn(async move { serve_stream(config, stream, addr).await });
+ }
+ }))
+ .await;
info!("serving http");
@@ -129,31 +126,29 @@ async fn serve_https(state: Arc<State>) -> Result<()> {
};
let tls_acceptor = Arc::new(TlsAcceptor::from(tls_config));
- let listen_futs: Result<Vec<()>> = try_join_all(https_config.bind
- .iter()
- .map(|e| async {
- let l = TcpListener::bind(e.clone()).await?;
- loop {
- let (stream, addr) = l.accept().await.context("accepting connection")?;
- let state = state.clone();
- let tls_acceptor = tls_acceptor.clone();
- tokio::task::spawn(async move {
- debug!("connection from {addr}");
- match tls_acceptor.accept(stream).await {
- Ok(stream) => serve_stream(state, stream, addr).await,
- Err(e) => warn!("error accepting tls: {e}"),
- };
- });
- }
- }))
- .await;
+ let listen_futs: Result<Vec<()>> = try_join_all(https_config.bind.iter().map(|e| async {
+ let l = TcpListener::bind(e.clone()).await?;
+ loop {
+ let (stream, addr) = l.accept().await.context("accepting connection")?;
+ let state = state.clone();
+ let tls_acceptor = tls_acceptor.clone();
+ tokio::task::spawn(async move {
+ debug!("connection from {addr}");
+ match tls_acceptor.accept(stream).await {
+ Ok(stream) => serve_stream(state, TokioIo(stream), addr).await,
+ Err(e) => warn!("error accepting tls: {e}"),
+ };
+ });
+ }
+ }))
+ .await;
info!("serving https");
listen_futs?;
Ok(())
}
-pub async fn serve_stream<T: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
+pub async fn serve_stream<T: Unpin + Send + 'static + hyper::rt::Read + hyper::rt::Write>(
state: Arc<State>,
stream: T,
addr: SocketAddr,