pub trait Listener {
type Server;
fn bind(
self,
addr: impl ToSocketAddrs
) -> Result<(SocketAddr, Self::Server)>;
fn listen(
self,
addr: impl ToSocketAddrs,
callback: impl Fn(SocketAddr)
) -> Result<Self::Server>;
fn run(self) -> Result<(SocketAddr, Self::Server)>;
}
This is supported on crate feature
tcp
only.Expand description
An app extension.
Associated Types
Required methods
fn bind(self, addr: impl ToSocketAddrs) -> Result<(SocketAddr, Self::Server)>
fn bind(self, addr: impl ToSocketAddrs) -> Result<(SocketAddr, Self::Server)>
Listen on a socket addr, return a server and the real addr it binds.
fn listen(
self,
addr: impl ToSocketAddrs,
callback: impl Fn(SocketAddr)
) -> Result<Self::Server>
fn listen(
self,
addr: impl ToSocketAddrs,
callback: impl Fn(SocketAddr)
) -> Result<Self::Server>
Listen on a socket addr, return a server, and pass real addr to the callback.
Listen on an unused port of 127.0.0.1, return a server and the real addr it binds.
Example
use roa::{App, Context, Status};
use roa::tcp::Listener;
use roa::http::StatusCode;
use tokio::task::spawn;
use std::time::Instant;
async fn end(_ctx: &mut Context) -> Result<(), Status> {
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (addr, server) = App::new().end(end).run()?;
spawn(server);
let resp = reqwest::get(&format!("http://{}", addr)).await?;
assert_eq!(StatusCode::OK, resp.status());
Ok(())
}