pub async fn serve(self, addr: &str) -> Result<(), ServerError> {
        let socketaddr: SocketAddr = addr.parse()?;

        let tcp_listener = TcpListener::bind(socketaddr).await?;
        loop {
            let (tcp_stream, sa) = tcp_listener.accept().await?;

            let s = self.clone();
            let sfn = service_fn(move |mut req: Request<Body>| {
                let ip = sa.ip();
                req.extensions_mut().insert(ip);
                let s = s.clone();
                async move { s.clone().dispatch(req).await }
            });

            #[cfg(all(feature = "logging", not(feature = "trace")))]
            log::trace!("Request from {}", sa);

            #[cfg(feature = "trace")]
            tracing::trace!("Request from {}", sa);

            tokio::task::spawn(async move {
                if let Err(http_err) = Http::new()
                    .http1_keep_alive(true)
                    .serve_connection(tcp_stream, sfn)
                    .await
                {
                    #[cfg(feature = "logging")]
                    log::error!("Error while serving HTTP connection: {}", http_err);
                    #[cfg(feature = "trace")]
                    tracing::error!("Error while serving HTTP connection: {}", http_err);
                    #[cfg(all(not(feature = "trace"), not(feature = "logging")))]
                    eprintln!("Error while serving HTTP connection: {}", http_err);
                }
            });
        }
    }
/// Start a TCP/HTTP server with tokio. Performs dispatch on an as-needed basis. This is a more
/// common path for users to start a server.
Start a TCP/HTTP server with tokio. Performs dispatch on an as-needed basis. This is a more
common path for users to start a server.
Start a TCP / HTTP server with tokio . Performs dispatch on an as - needed basis . This is a more common path for users to start a server .
pub async fn serve(self, addr: &str) -> Result<(), ServerError> {
        let socketaddr: SocketAddr = addr.parse()?;
        let tcp_listener = TcpListener::bind(socketaddr).await?;
        loop {
            let (tcp_stream, sa) = tcp_listener.accept().await?;
            let s = self.clone();
            let sfn = service_fn(move |mut req: Request<Body>| {
                let ip = sa.ip();
                req.extensions_mut().insert(ip);
                let s = s.clone();
                async move { s.clone().dispatch(req).await }
            });
            #[cfg(all(feature = "logging", not(feature = "trace")))]
            log::trace!("Request from {}", sa);
            #[cfg(feature = "trace")]
            tracing::trace!("Request from {}", sa);
            tokio::task::spawn(async move {
                if let Err(http_err) = Http::new()
                    .http1_keep_alive(true)
                    .serve_connection(tcp_stream, sfn)
                    .await
                {
                    #[cfg(feature = "logging")]
                    log::error!("Error while serving HTTP connection: {}", http_err);
                    #[cfg(feature = "trace")]
                    tracing::error!("Error while serving HTTP connection: {}", http_err);
                    #[cfg(all(not(feature = "trace"), not(feature = "logging")))]
                    eprintln!("Error while serving HTTP connection: {}", http_err);
                }
            });
        }
    }
pub async fn serve ( self , addr : & str ) -> Result < ( ) , ServerError > { let socketaddr : SocketAddr = addr . parse ( ) ? ; let tcp_listener = TcpListener :: bind ( socketaddr ) . await ? ; loop { let ( tcp_stream , sa ) = tcp_listener . accept ( ) . await ? ; let s = self . clone ( ) ; let sfn = service_fn ( move | mut req : Request < Body > | { let ip = sa . ip ( ) ; req . extensions_mut ( ) . insert ( ip ) ; let s = s . clone ( ) ; async move { s . clone ( ) . dispatch ( req ) . await } } ) ; # [ cfg ( all ( feature " " not ( feature " " ) ) ) ] log :: trace ! ( " " sa ) ; # [ cfg ( feature " " ) ] tracing :: trace ! ( " " sa ) ; tokio :: task :: spawn ( async move { if let Err ( http_err ) = Http :: new ( ) . http1_keep_alive ( true ) . serve_connection ( tcp_stream , sfn ) . await { # [ cfg ( feature " " ) ] log :: error ! ( " " http_err ) ; # [ cfg ( feature " " ) ] tracing :: error ! ( " " http_err ) ; # [ cfg ( all ( not ( feature " " ) not ( feature " " ) ) ) ] eprintln ! ( " " http_err ) ; } } ) ; } }
Start a TCP/HTTP server with tokio.
Start a TCP / HTTP server with tokio .
self
addr
&str
self
addr
&str