[][src]Trait roa_tcp::Listener

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)>; }

An app extension.

Associated Types

type Server

http server

Loading content...

Required methods

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>

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, Context, Error};
use roa_tcp::Listener;
use roa_core::http::StatusCode;
use async_std::task::spawn;
use std::time::Instant;

async fn end(_ctx: &mut Context<()>) -> Result<(), Error> {
    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(())
}
Loading content...

Implementations on Foreign Types

impl<S, E> Listener for App<S, Arc<E>> where
    S: State,
    E: for<'a> Endpoint<'a, S>, 
[src]

type Server = Server<TcpIncoming, Self, Executor>

Loading content...

Implementors

Loading content...