Skip to content

Commit

Permalink
add from_tcp methods
Browse files Browse the repository at this point in the history
  • Loading branch information
programatik29 committed Aug 5, 2022
1 parent 1a68546 commit dfafcbd
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ The format is based on [Keep a Changelog], and this project adheres to

# Unreleased

None.
- **added:** Added `Server::from_tcp`, `axum_server::from_tcp` and
`axum_server::from_tcp_rustls` methods to create `Server` from
`std::net::TcpListener`.

# 0.4.1 (29. July 2022)

Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ all-features = true
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples=examples"]
rustdoc-args = ["--cfg", "docsrs"]

[[example]]
name = "from_std_listener_rustls"
required-features = ["tls-rustls"]

[[example]]
name = "http_and_https"
required-features = ["tls-rustls"]
Expand Down
19 changes: 19 additions & 0 deletions examples/from_std_listener.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Run with `cargo run --example from_std_listener` command.
//!
//! To connect through browser, navigate to "http://localhost:3000" url.
use axum::{routing::get, Router};
use std::net::{SocketAddr, TcpListener};

#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = TcpListener::bind(addr).unwrap();
println!("listening on {}", addr);
axum_server::from_tcp(listener)
.serve(app.into_make_service())
.await
.unwrap();
}
28 changes: 28 additions & 0 deletions examples/from_std_listener_rustls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Run with `cargo run --all-features --example from_std_listener_rustls`
//! command.
//!
//! To connect through browser, navigate to "https://localhost:3000" url.
use axum::{routing::get, Router};
use axum_server::tls_rustls::RustlsConfig;
use std::net::{SocketAddr, TcpListener};

#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));

let config = RustlsConfig::from_pem_file(
"examples/self-signed-certs/cert.pem",
"examples/self-signed-certs/key.pem",
)
.await
.unwrap();

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = TcpListener::bind(addr).unwrap();
println!("listening on {}", addr);
axum_server::from_tcp_rustls(listener, config)
.serve(app.into_make_service())
.await
.unwrap();
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub use self::{
addr_incoming_config::AddrIncomingConfig,
handle::Handle,
http_config::HttpConfig,
server::{bind, Server},
server::{bind, from_tcp, Server},
};

#[cfg(feature = "tls-rustls")]
Expand All @@ -109,4 +109,4 @@ pub mod tls_rustls;

#[doc(inline)]
#[cfg(feature = "tls-rustls")]
pub use self::tls_rustls::export::bind_rustls;
pub use self::tls_rustls::export::{bind_rustls, from_tcp_rustls};
42 changes: 35 additions & 7 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,28 @@ use tokio::{
#[derive(Debug)]
pub struct Server<A = DefaultAcceptor> {
acceptor: A,
addr: SocketAddr,
listener: Listener,
addr_incoming_conf: AddrIncomingConfig,
handle: Handle,
http_conf: HttpConfig,
}

#[derive(Debug)]
enum Listener {
Bind(SocketAddr),
Std(std::net::TcpListener),
}

/// Create a [`Server`] that will bind to provided address.
pub fn bind(addr: SocketAddr) -> Server {
Server::bind(addr)
}

/// Create a [`Server`] from existing `std::net::TcpListener`.
pub fn from_tcp(listener: std::net::TcpListener) -> Server {
Server::from_tcp(listener)
}

impl Server {
/// Create a server that will bind to provided address.
pub fn bind(addr: SocketAddr) -> Self {
Expand All @@ -44,7 +55,21 @@ impl Server {

Self {
acceptor,
addr,
listener: Listener::Bind(addr),
addr_incoming_conf: AddrIncomingConfig::default(),
handle,
http_conf: HttpConfig::default(),
}
}

/// Create a server from existing `std::net::TcpListener`.
pub fn from_tcp(listener: std::net::TcpListener) -> Self {
let acceptor = DefaultAcceptor::new();
let handle = Handle::new();

Self {
acceptor,
listener: Listener::Std(listener),
addr_incoming_conf: AddrIncomingConfig::default(),
handle,
http_conf: HttpConfig::default(),
Expand All @@ -57,7 +82,7 @@ impl<A> Server<A> {
pub fn acceptor<Acceptor>(self, acceptor: Acceptor) -> Server<Acceptor> {
Server {
acceptor,
addr: self.addr,
listener: self.listener,
addr_incoming_conf: self.addr_incoming_conf,
handle: self.handle,
http_conf: self.http_conf,
Expand All @@ -71,7 +96,7 @@ impl<A> Server<A> {
{
Server {
acceptor: acceptor(self.acceptor),
addr: self.addr,
listener: self.listener,
addr_incoming_conf: self.addr_incoming_conf,
handle: self.handle,
http_conf: self.http_conf,
Expand Down Expand Up @@ -134,7 +159,7 @@ impl<A> Server<A> {
let handle = self.handle;
let http_conf = self.http_conf;

let mut incoming = match bind_incoming(self.addr, addr_incoming_conf).await {
let mut incoming = match bind_incoming(self.listener, addr_incoming_conf).await {
Ok(v) => v,
Err(e) => {
handle.notify_listening(None);
Expand Down Expand Up @@ -202,10 +227,13 @@ impl<A> Server<A> {
}

async fn bind_incoming(
addr: SocketAddr,
listener: Listener,
addr_incoming_conf: AddrIncomingConfig,
) -> io::Result<AddrIncoming> {
let listener = TcpListener::bind(addr).await?;
let listener = match listener {
Listener::Bind(addr) => TcpListener::bind(addr).await?,
Listener::Std(std_listener) => TcpListener::from_std(std_listener)?,
};
let mut incoming = AddrIncoming::from_listener(listener).map_err(io_other)?;

incoming.set_sleep_on_errors(addr_incoming_conf.tcp_sleep_on_accept_errors);
Expand Down
21 changes: 21 additions & 0 deletions src/tls_rustls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ pub(crate) mod export {
pub fn bind_rustls(addr: SocketAddr, config: RustlsConfig) -> Server<RustlsAcceptor> {
super::bind_rustls(addr, config)
}

/// Create a tls server from existing `std::net::TcpListener`.
#[cfg_attr(docsrs, doc(cfg(feature = "tls-rustls")))]
pub fn from_tcp_rustls(
listener: std::net::TcpListener,
config: RustlsConfig,
) -> Server<RustlsAcceptor> {
let acceptor = RustlsAcceptor::new(config);

Server::from_tcp(listener).acceptor(acceptor)
}
}

pub mod future;
Expand All @@ -61,6 +72,16 @@ pub fn bind_rustls(addr: SocketAddr, config: RustlsConfig) -> Server<RustlsAccep
Server::bind(addr).acceptor(acceptor)
}

/// Create a tls server from existing `std::net::TcpListener`.
pub fn from_tcp_rustls(
listener: std::net::TcpListener,
config: RustlsConfig,
) -> Server<RustlsAcceptor> {
let acceptor = RustlsAcceptor::new(config);

Server::from_tcp(listener).acceptor(acceptor)
}

/// Tls acceptor using rustls.
#[derive(Clone)]
pub struct RustlsAcceptor<A = DefaultAcceptor> {
Expand Down

0 comments on commit dfafcbd

Please sign in to comment.