Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions ohkami/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
fn ino_tcp_listener(self) -> impl Future<Output = TcpListener>;
}
impl IntoTcpListener<()> for TcpListener {
async fn ino_tcp_listener(self) -> TcpListener {
self
}
}
impl<A: ToSocketAddrs> IntoTcpListener<A> 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")]
Expand Down
43 changes: 35 additions & 8 deletions ohkami/src/ohkami/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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<T>(self, bind: impl __rt__::IntoTcpListener<T>) {
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());

Expand Down