diff --git a/ohkami/src/lib.rs b/ohkami/src/lib.rs index 2481431bc..35785aad3 100644 --- a/ohkami/src/lib.rs +++ b/ohkami/src/lib.rs @@ -48,13 +48,23 @@ mod __rt__ { #[cfg(feature="rt_glommio")] pub(crate) use {glommio::net::{TcpListener, TcpStream}, std::net::ToSocketAddrs}; - pub(crate) async fn bind(address: impl ToSocketAddrs) -> TcpListener { - let binded = TcpListener::bind(address); - - #[cfg(any(feature="rt_tokio", feature="rt_async-std", feature="rt_smol", feature="rt_nio"))] - let binded = binded.await; - - binded.expect("Failed to bind TCP listener") + pub trait IntoTcpListener { + fn ino_tcp_listener(self) -> impl Future; + } + impl IntoTcpListener<()> for TcpListener { + async fn ino_tcp_listener(self) -> TcpListener { + self + } + } + impl IntoTcpListener for A { + async fn ino_tcp_listener(self) -> TcpListener { + let binded = TcpListener::bind(self); + + #[cfg(not(feature="rt_glommio"))] + let binded = binded.await; + + binded.expect("Failed to bind TCP listener") + } } #[cfg(feature="rt_tokio")] diff --git a/ohkami/src/ohkami/mod.rs b/ohkami/src/ohkami/mod.rs index 54a003a2b..2e8d44490 100644 --- a/ohkami/src/ohkami/mod.rs +++ b/ohkami/src/ohkami/mod.rs @@ -459,14 +459,16 @@ impl Ohkami { } #[cfg(feature="__rt_native__")] - /// Start serving at `address`! + /// Bind this `Ohkami` to an address and start serving ! /// - /// `address` is: + /// `bind` is: /// - /// - `tokio::net::ToSocketAddrs` if using `tokio` - /// - `async_std::net::ToSocketAddrs` if using `async-std` - /// - `smol::net::AsyncToSocketAddrs` if using `smol` - /// - `std::net::ToSocketAddrs` if using `nio` or `glommio` + /// - `tokio::net::ToSocketAddrs` item or `tokio::net::TcpListener` + /// - `async_std::net::ToSocketAddrs` item or `async_std::net::TcpListener` + /// - `smol::net::AsyncToSocketAddrs` item or `smol::net::TcpListener` + /// - `std::net::ToSocketAddrs` item or `{glommio, nio}::net::TcpListener` + /// + /// for each async runtime. /// /// *note* : Keep-Alive timeout is 42 seconds by default. /// This is configureable by `OHKAMI_KEEPALIVE_TIMEOUT` @@ -519,11 +521,36 @@ impl Ohkami { /// }).unwrap().join_all(); /// } /// ``` - pub async fn howl(self, address: impl __rt__::ToSocketAddrs) { + /// + /// --- + /// + /// *example_with_tcp_listener.rs* + /// ```no_run + /// use ohkami::prelude::*; + /// use tokio::net::TcpSocket; // <--- + /// + /// #[tokio::main] + /// async fn main() -> std::io::Result<()> { + /// let socket = TcpSocket::new_v4()?; + /// + /// socket.bind("0.0.0.0:5000".parse().unwrap())?; + /// + /// let listener = socket.listen(1024)?; + /// + /// Ohkami::new(( + /// "/".GET(async || { + /// "Hello, TcpListener!" + /// }), + /// )).howl(listener).await; + /// + /// Ok(()) + /// } + /// ``` + pub async fn howl(self, bind: impl __rt__::IntoTcpListener) { let (router, _) = self.into_router().finalize(); let router = Arc::new(router); - let listener = __rt__::bind(address).await; + let listener = bind.ino_tcp_listener().await; let (wg, ctrl_c) = (sync::WaitGroup::new(), sync::CtrlC::new());