pub async fn run(self) -> Result<(), SaphirError> {
        let Server { listener_config, stack } = self;

        if INIT_STACK.state() != OnceState::New {
            return Err(SaphirError::Other("cannot run a second server".to_owned()));
        }

        INIT_STACK.call_once(|| {
            // # SAFETY #
            // We write only once in the static memory. No override.
            // Above check also make sure there is no second server.
            unsafe { STACK.as_mut_ptr().write(stack); }
        });

        // # SAFETY #
        // Memory has been initialized above.
        let stack = unsafe { STACK.as_ptr().as_ref().expect("Memory has been initialized above.") };

        let http = Http::new();

        let mut listener = TcpListener::bind(listener_config.iface.clone()).await?;
        let local_addr = listener.local_addr()?;

        let incoming = {
            #[cfg(feature = "https")]
                {
                    use crate::server::ssl_loading_utils::MaybeTlsAcceptor;
                    match listener_config.ssl_config() {
                        (Some(cert_config), Some(key_config)) => {
                            use std::sync::Arc;
                            use crate::server::ssl_loading_utils::*;
                            use tokio_rustls::TlsAcceptor;

                            let certs = load_certs(&cert_config);
                            let key = load_private_key(&key_config);
                            let mut cfg = ::rustls::ServerConfig::new(::rustls::NoClientAuth::new());
                            let _ = cfg.set_single_cert(certs, key);
                            let arc_config = Arc::new(cfg);

                            let acceptor = TlsAcceptor::from(arc_config);

                            let inc = listener.incoming().and_then(move |stream| {
                                acceptor.accept(stream)
                            });

                            info!("Saphir started and listening on : https://{}", local_addr);

                            MaybeTlsAcceptor::Tls(Box::pin(inc))
                        }
                        (cert_config, key_config) if cert_config.xor(key_config).is_some() => {
                            return Err(SaphirError::Other("Invalid SSL configuration, missing cert or key".to_string()));
                        }
                        _ => {
                            let incoming = listener.incoming();
                            info!("Saphir started and listening on : http://{}", local_addr);
                            MaybeTlsAcceptor::Plain(Box::pin(incoming))
                        }
                    }
                }

            #[cfg(not(feature = "https"))]
                {
                    info!("Saphir started and listening on : http://{}", local_addr);
                    listener.incoming()
                }
        };

        if let Some(request_timeout_ms) = listener_config.request_timeout_ms {
            use tokio::time::{Duration, timeout};
            incoming.for_each_concurrent(None, |client_socket| async {
                match client_socket {
                    Ok(client_socket) => {
                        let peer_addr = client_socket.peer_addr().ok();
                        let http_handler = http.serve_connection(client_socket, stack.new_handler(peer_addr));
                        let f = timeout(Duration::from_millis(request_timeout_ms), http_handler);

                        tokio::spawn(f);
                    }
                    Err(e) => {
                        warn!("incoming connection encountered an error: {}", e);
                    }
                }
            }).await;
        } else {
            incoming.for_each_concurrent(None, |client_socket| async {
                match client_socket {
                    Ok(client_socket) => {
                        let peer_addr = client_socket.peer_addr().ok();
                        let http_handler = http.serve_connection(client_socket, stack.new_handler(peer_addr));

                        tokio::spawn(http_handler);
                    }
                    Err(e) => {
                        warn!("incoming connection encountered an error: {}", e);
                    }
                }
            }).await;
        }

        Ok(())
    }
/// Return a future with will run the server. Simply run this future inside the tokio executor
/// or await it in a async context
Return a future with will run the server. Simply run this future inside the tokio executor
or await it in a async context
Return a future with will run the server . Simply run this future inside the tokio executor or await it in a async context
pub async fn run(self) -> Result<(), SaphirError> {
        let Server { listener_config, stack } = self;
        if INIT_STACK.state() != OnceState::New {
            return Err(SaphirError::Other("cannot run a second server".to_owned()));
        }
        INIT_STACK.call_once(|| {
            // # SAFETY #
            // We write only once in the static memory. No override.
            // Above check also make sure there is no second server.
            unsafe { STACK.as_mut_ptr().write(stack); }
        });
        // # SAFETY #
        // Memory has been initialized above.
        let stack = unsafe { STACK.as_ptr().as_ref().expect("Memory has been initialized above.") };
        let http = Http::new();
        let mut listener = TcpListener::bind(listener_config.iface.clone()).await?;
        let local_addr = listener.local_addr()?;
        let incoming = {
            #[cfg(feature = "https")]
                {
                    use crate::server::ssl_loading_utils::MaybeTlsAcceptor;
                    match listener_config.ssl_config() {
                        (Some(cert_config), Some(key_config)) => {
                            use std::sync::Arc;
                            use crate::server::ssl_loading_utils::*;
                            use tokio_rustls::TlsAcceptor;
                            let certs = load_certs(&cert_config);
                            let key = load_private_key(&key_config);
                            let mut cfg = ::rustls::ServerConfig::new(::rustls::NoClientAuth::new());
                            let _ = cfg.set_single_cert(certs, key);
                            let arc_config = Arc::new(cfg);
                            let acceptor = TlsAcceptor::from(arc_config);
                            let inc = listener.incoming().and_then(move |stream| {
                                acceptor.accept(stream)
                            });
                            info!("Saphir started and listening on : https://{}", local_addr);
                            MaybeTlsAcceptor::Tls(Box::pin(inc))
                        }
                        (cert_config, key_config) if cert_config.xor(key_config).is_some() => {
                            return Err(SaphirError::Other("Invalid SSL configuration, missing cert or key".to_string()));
                        }
                        _ => {
                            let incoming = listener.incoming();
                            info!("Saphir started and listening on : http://{}", local_addr);
                            MaybeTlsAcceptor::Plain(Box::pin(incoming))
                        }
                    }
                }
            #[cfg(not(feature = "https"))]
                {
                    info!("Saphir started and listening on : http://{}", local_addr);
                    listener.incoming()
                }
        };
        if let Some(request_timeout_ms) = listener_config.request_timeout_ms {
            use tokio::time::{Duration, timeout};
            incoming.for_each_concurrent(None, |client_socket| async {
                match client_socket {
                    Ok(client_socket) => {
                        let peer_addr = client_socket.peer_addr().ok();
                        let http_handler = http.serve_connection(client_socket, stack.new_handler(peer_addr));
                        let f = timeout(Duration::from_millis(request_timeout_ms), http_handler);
                        tokio::spawn(f);
                    }
                    Err(e) => {
                        warn!("incoming connection encountered an error: {}", e);
                    }
                }
            }).await;
        } else {
            incoming.for_each_concurrent(None, |client_socket| async {
                match client_socket {
                    Ok(client_socket) => {
                        let peer_addr = client_socket.peer_addr().ok();
                        let http_handler = http.serve_connection(client_socket, stack.new_handler(peer_addr));
                        tokio::spawn(http_handler);
                    }
                    Err(e) => {
                        warn!("incoming connection encountered an error: {}", e);
                    }
                }
            }).await;
        }
        Ok(())
    }
pub async fn run ( self ) -> Result < ( ) , SaphirError > { let Server { listener_config , stack } = self ; if INIT_STACK . state ( ) != OnceState :: New { return Err ( SaphirError :: Other ( " " . to_owned ( ) ) ) ; } INIT_STACK . call_once ( | | { // # SAFETY # // We write only once in the static memory. No override. // Above check also make sure there is no second server. unsafe { STACK . as_mut_ptr ( ) . write ( stack ) ; } } ) ; // # SAFETY # // Memory has been initialized above. let stack = unsafe { STACK . as_ptr ( ) . as_ref ( ) . expect ( " " ) } ; let http = Http :: new ( ) ; let mut listener = TcpListener :: bind ( listener_config . iface . clone ( ) ) . await ? ; let local_addr = listener . local_addr ( ) ? ; let incoming = { # [ cfg ( feature " " ) ] { use crate :: server :: ssl_loading_utils :: MaybeTlsAcceptor ; match listener_config . ssl_config ( ) { ( Some ( cert_config ) , Some ( key_config ) ) => { use std :: sync :: Arc ; use crate :: server :: ssl_loading_utils :: * ; use tokio_rustls :: TlsAcceptor ; let certs = load_certs ( & cert_config ) ; let key = load_private_key ( & key_config ) ; let mut cfg = :: rustls :: ServerConfig :: new ( :: rustls :: NoClientAuth :: new ( ) ) ; let _ = cfg . set_single_cert ( certs , key ) ; let arc_config = Arc :: new ( cfg ) ; let acceptor = TlsAcceptor :: from ( arc_config ) ; let inc = listener . incoming ( ) . and_then ( move | stream | { acceptor . accept ( stream ) } ) ; info ! ( " " local_addr ) ; MaybeTlsAcceptor :: Tls ( Box :: pin ( inc ) ) } ( cert_config , key_config ) if cert_config . xor ( key_config ) . is_some ( ) => { return Err ( SaphirError :: Other ( " " . to_string ( ) ) ) ; } _ => { let incoming = listener . incoming ( ) ; info ! ( " " local_addr ) ; MaybeTlsAcceptor :: Plain ( Box :: pin ( incoming ) ) } } } # [ cfg ( not ( feature " " ) ) ] { info ! ( " " local_addr ) ; listener . incoming ( ) } } ; if let Some ( request_timeout_ms ) = listener_config . request_timeout_ms { use tokio :: time :: { Duration , timeout } ; incoming . for_each_concurrent ( None , | client_socket | async { match client_socket { Ok ( client_socket ) => { let peer_addr = client_socket . peer_addr ( ) . ok ( ) ; let http_handler = http . serve_connection ( client_socket , stack . new_handler ( peer_addr ) ) ; let f = timeout ( Duration :: from_millis ( request_timeout_ms ) , http_handler ) ; tokio :: spawn ( f ) ; } Err ( e ) => { warn ! ( " " e ) ; } } } ) . await ; } else { incoming . for_each_concurrent ( None , | client_socket | async { match client_socket { Ok ( client_socket ) => { let peer_addr = client_socket . peer_addr ( ) . ok ( ) ; let http_handler = http . serve_connection ( client_socket , stack . new_handler ( peer_addr ) ) ; tokio :: spawn ( http_handler ) ; } Err ( e ) => { warn ! ( " " e ) ; } } } ) . await ; } Ok ( ( ) ) }
Return a future with will run the server.
Return a future with will run the server .
self
self