[][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)>; }
This is supported on feature="tcp" only.

An app extension.

Associated Types

type Server

This is supported on feature="tcp" only.

http server

Loading content...

Required methods

fn bind(self, addr: impl ToSocketAddrs) -> Result<(SocketAddr, Self::Server)>

This is supported on feature="tcp" only.

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>

This is supported on feature="tcp" only.

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

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

This is supported on feature="tcp" only.

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 async_std::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(())
}
Loading content...

Implementors

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>

This is supported on feature="tcp" only.
Loading content...