[][src]Trait roa_tcp::Listener

pub trait Listener {
    type Server;
    fn listen_on(
        &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)>; }

An app extension.

Associated Types

type Server

http server

Loading content...

Required methods

fn listen_on(
    &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>

Listen on a socket addr, return a server, and pass real addr to the callback.

fn run(&self) -> Result<(SocketAddr, Self::Server)>

Listen on an unused port of 127.0.0.1, return a server and the real addr it binds.

Example

use roa_core::App;
use roa_tcp::Listener;
use roa_core::http::StatusCode;
use async_std::task::spawn;
use std::time::Instant;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (addr, server) = App::new(())
        .gate_fn(|_ctx, next| async move {
            let inbound = Instant::now();
            next.await?;
            println!("time elapsed: {} ms", inbound.elapsed().as_millis());
            Ok(())
        })
        .run()?;
    spawn(server);
    let resp = reqwest::get(&format!("http://{}", addr)).await?;
    assert_eq!(StatusCode::OK, resp.status());
    Ok(())
}
Loading content...

Implementations on Foreign Types

impl<S: State> Listener for App<S>[src]

type Server = Server<TcpIncoming, Self, Executor>

Loading content...

Implementors

Loading content...