Skip to content

Commit

Permalink
chore(viz): add custom Listener Trait back
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Jan 2, 2024
1 parent c143cdf commit b7419c1
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 46 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ thiserror = "1.0"
# router
path-tree = "0.7.4"

# session
sessions = "0.6"
sessions-core = "0.6"
sessions-memory = "0.6"

# http
headers = "0.4"
http = "1"
Expand Down
2 changes: 1 addition & 1 deletion examples/rustls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> Result<()> {

let app = Router::new().route("/", get(index));

let listener = tls::Listener::<_, tls::rustls::TlsAcceptor>::new(
let listener = tls::TlsListener::<_, tls::rustls::TlsAcceptor>::new(
listener,
tls::rustls::Config::new()
.cert(include_bytes!("../../tls/cert.pem").to_vec())
Expand Down
2 changes: 1 addition & 1 deletion examples/session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ viz = { workspace = true, features = ["session"] }

tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

sessions = { version = "0.6.0", features = ["memory"] }
sessions = { workspace = true, features = ["memory"] }
nano-id = "0.3"
2 changes: 1 addition & 1 deletion viz-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ form-data = { version = "0.5.3", optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
serde_json = { workspace = true, optional = true }
serde_urlencoded = { workspace = true, optional = true }
sessions-core = { version = "0.6", optional = true }
sessions-core = { workspace = true, optional = true }

# CSRF
getrandom = { version = "0.2", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion viz-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ http-body-util.workspace = true
hyper.workspace = true
mime.workspace = true
serde.workspace = true
sessions = { version = "0.5", features = ["memory"] }
sessions = { workspace = true, features = ["memory"] }
nano-id = "0.3"

http = "=0.2"
Expand Down
13 changes: 5 additions & 8 deletions viz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,19 +522,19 @@
))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[cfg(any(feature = "http1", feature = "http2"))]
mod responder;
#[cfg(any(feature = "http1", feature = "http2"))]
pub use responder::Responder;
#[cfg(any(feature = "http1", feature = "http2"))]

mod server;
#[cfg(any(feature = "http1", feature = "http2"))]
pub use server::{serve, Server};
pub use server::{serve, Listener, Server};

/// TLS
#[cfg(any(feature = "native_tls", feature = "rustls"))]
pub mod tls;

pub use viz_core::*;
pub use viz_router::*;

#[cfg(feature = "handlers")]
#[cfg_attr(docsrs, doc(cfg(feature = "handlers")))]
#[doc(inline)]
Expand All @@ -544,6 +544,3 @@ pub use viz_handlers as handlers;
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
#[doc(inline)]
pub use viz_macros::handler;

pub use viz_core::*;
pub use viz_router::*;
12 changes: 10 additions & 2 deletions viz/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ use hyper_util::{
server::conn::auto::Builder,
};
use tokio::{pin, select, sync::watch};
use tokio_util::net::Listener;

use crate::{future::FutureExt, Responder, Router, Tree};

mod listener;
pub use listener::Listener;

#[cfg(any(feature = "http1", feature = "http2"))]
mod tcp;

#[cfg(all(unix, feature = "unix-socket"))]
mod unix;

/// Starts a server and serves the connections.
pub fn serve<L>(listener: L, router: Router) -> Server<L>
where
Expand Down Expand Up @@ -77,7 +85,7 @@ where
tree,
signal,
builder,
mut listener,
listener,
} = self;

let (shutdown_tx, shutdown_rx) = watch::channel(());
Expand Down
17 changes: 17 additions & 0 deletions viz/src/server/listener.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::{future::Future, io::Result};

use tokio::io::{AsyncRead, AsyncWrite};

/// A trait for a listener: `TcpListener` and `UnixListener`.
pub trait Listener {
/// The stream's type of this listener.
type Io: AsyncRead + AsyncWrite;
/// The socket address type of this listener.
type Addr;

/// Accepts a new incoming connection from this listener.
fn accept(&self) -> impl Future<Output = Result<(Self::Io, Self::Addr)>> + Send;

/// Returns the local address that this listener is bound to.
fn local_addr(&self) -> Result<Self::Addr>;
}
16 changes: 16 additions & 0 deletions viz/src/server/tcp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::{future::Future, io::Result, net::SocketAddr};

use tokio::net::{TcpListener, TcpStream};

impl super::Listener for TcpListener {
type Io = TcpStream;
type Addr = SocketAddr;

fn accept(&self) -> impl Future<Output = Result<(Self::Io, Self::Addr)>> + Send {
TcpListener::accept(self)
}

fn local_addr(&self) -> Result<Self::Addr> {
TcpListener::local_addr(self)
}
}
16 changes: 16 additions & 0 deletions viz/src/server/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::{future::Future, io::Result};

use tokio::net::{unix::SocketAddr, UnixListener, UnixStream};

impl super::Listener for UnixListener {
type Io = UnixStream;
type Addr = SocketAddr;

fn accept(&self) -> impl Future<Output = Result<(Self::Io, Self::Addr)>> + Send {
UnixListener::accept(self)
}

fn local_addr(&self) -> Result<Self::Addr> {
UnixListener::local_addr(self)
}
}
2 changes: 1 addition & 1 deletion viz/src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod listener;

pub use listener::Listener;
pub use listener::TlsListener;

/// `native_tls`
#[cfg(feature = "native-tls")]
Expand Down
4 changes: 2 additions & 2 deletions viz/src/tls/listener.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// Unified TLS listener type.
#[derive(Debug)]
pub struct Listener<T, A> {
pub struct TlsListener<T, A> {
pub(crate) inner: T,
pub(crate) acceptor: A,
}

impl<T, A> Listener<T, A> {
impl<T, A> TlsListener<T, A> {
/// Creates a new TLS listener.
pub fn new(t: T, a: A) -> Self {
Self {
Expand Down
27 changes: 10 additions & 17 deletions viz/src/tls/native_tls.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use std::{
fmt,
io::{Error as IoError, ErrorKind, Result as IoResult},
net::SocketAddr,
task::{Context, Poll},
};
use std::{fmt, io::Result as IoResult, net::SocketAddr};

use futures_util::FutureExt;
use tokio::net::{TcpListener, TcpStream};
use tokio_native_tls::{native_tls::TlsAcceptor as TlsAcceptorWrapper, TlsStream};

use super::Listener;
use crate::{Error, Result};

pub use tokio_native_tls::{native_tls::Identity, TlsAcceptor};
Expand Down Expand Up @@ -44,18 +37,18 @@ impl Config {
}
}

impl tokio_util::net::Listener for Listener<TcpListener, TlsAcceptor> {
impl crate::Listener for super::TlsListener<TcpListener, TlsAcceptor> {
type Io = TlsStream<TcpStream>;
type Addr = SocketAddr;

fn poll_accept(&mut self, cx: &mut Context<'_>) -> Poll<IoResult<(Self::Io, Self::Addr)>> {
let Poll::Ready((stream, addr)) = self.inner.poll_accept(cx)? else {
return Poll::Pending;
};
Box::pin(self.acceptor.accept(stream))
.poll_unpin(cx)
.map_ok(|stream| (stream, addr))
.map_err(|e| IoError::new(ErrorKind::Other, e))
async fn accept(&self) -> IoResult<(Self::Io, Self::Addr)> {
let (stream, addr) = self.inner.accept().await?;
let stream = self
.acceptor
.accept(stream)
.await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok((stream, addr))
}

fn local_addr(&self) -> IoResult<Self::Addr> {
Expand Down
17 changes: 5 additions & 12 deletions viz/src/tls/rustls.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::{
io::{Error as IoError, ErrorKind, Result as IoResult},
net::SocketAddr,
task::{Context, Poll},
};

use futures_util::FutureExt;
use tokio::net::{TcpListener, TcpStream};
use tokio_rustls::{
rustls::{
Expand All @@ -16,7 +14,6 @@ use tokio_rustls::{
server::TlsStream,
};

use super::Listener;
use crate::{Error, Result};

pub use tokio_rustls::TlsAcceptor;
Expand Down Expand Up @@ -151,18 +148,14 @@ impl Config {
}
}

impl tokio_util::net::Listener for Listener<TcpListener, TlsAcceptor> {
impl crate::Listener for super::TlsListener<TcpListener, TlsAcceptor> {
type Io = TlsStream<TcpStream>;
type Addr = SocketAddr;

fn poll_accept(&mut self, cx: &mut Context<'_>) -> Poll<IoResult<(Self::Io, Self::Addr)>> {
let Poll::Ready((stream, addr)) = self.inner.poll_accept(cx)? else {
return Poll::Pending;
};
self.acceptor
.accept(stream)
.poll_unpin(cx)
.map_ok(|stream| (stream, addr))
async fn accept(&self) -> IoResult<(Self::Io, Self::Addr)> {
let (stream, addr) = self.inner.accept().await?;
let stream = self.acceptor.accept(stream).await?;
Ok((stream, addr))
}

fn local_addr(&self) -> IoResult<Self::Addr> {
Expand Down

0 comments on commit b7419c1

Please sign in to comment.