Struct warp::Server

source ·
pub struct Server<F> { /* private fields */ }
Expand description

A Warp Server ready to filter requests.

Implementations§

source§

impl<F> Server<F>
where F: Filter + Clone + Send + Sync + 'static, <F::Future as TryFuture>::Ok: Reply, <F::Future as TryFuture>::Error: IsReject,

source

pub async fn run(self, addr: impl Into<SocketAddr>)

Run this Server forever on the current thread.

§Panics

Panics if we are unable to bind to the provided address.

source

pub async fn run_incoming<I>(self, incoming: I)
where I: TryStream + Send, I::Ok: AsyncRead + AsyncWrite + Send + 'static + Unpin, I::Error: Into<Box<dyn StdError + Send + Sync>>,

Run this Server forever on the current thread with a specific stream of incoming connections.

This can be used for Unix Domain Sockets, or TLS, etc.

source

pub fn bind( self, addr: impl Into<SocketAddr> + 'static, ) -> impl Future<Output = ()> + 'static

Bind to a socket address, returning a Future that can be executed on the current runtime.

§Panics

Panics if we are unable to bind to the provided address.

source

pub async fn try_bind(self, addr: impl Into<SocketAddr>)

Bind to a socket address, returning a Future that can be executed on any runtime.

In case we are unable to bind to the specified address, resolves to an error and logs the reason.

source

pub fn bind_ephemeral( self, addr: impl Into<SocketAddr>, ) -> (SocketAddr, impl Future<Output = ()> + 'static)

Bind to a possibly ephemeral socket address.

Returns the bound address and a Future that can be executed on the current runtime.

§Panics

Panics if we are unable to bind to the provided address.

source

pub fn try_bind_ephemeral( self, addr: impl Into<SocketAddr>, ) -> Result<(SocketAddr, impl Future<Output = ()> + 'static), Error>

Tried to bind a possibly ephemeral socket address.

Returns a Result which fails in case we are unable to bind with the underlying error.

Returns the bound address and a Future that can be executed on the current runtime.

source

pub fn bind_with_graceful_shutdown( self, addr: impl Into<SocketAddr> + 'static, signal: impl Future<Output = ()> + Send + 'static, ) -> (SocketAddr, impl Future<Output = ()> + 'static)

Create a server with graceful shutdown signal.

When the signal completes, the server will start the graceful shutdown process.

Returns the bound address and a Future that can be executed on the current runtime.

§Example
use warp::Filter;
use futures_util::future::TryFutureExt;
use tokio::sync::oneshot;

let routes = warp::any()
    .map(|| "Hello, World!");

let (tx, rx) = oneshot::channel();

let (addr, server) = warp::serve(routes)
    .bind_with_graceful_shutdown(([127, 0, 0, 1], 3030), async {
         rx.await.ok();
    });

// Spawn the server into a runtime
tokio::task::spawn(server);

// Later, start the shutdown...
let _ = tx.send(());
§Panics

Panics if we are unable to bind to the provided address.

source

pub fn try_bind_with_graceful_shutdown( self, addr: impl Into<SocketAddr> + 'static, signal: impl Future<Output = ()> + Send + 'static, ) -> Result<(SocketAddr, impl Future<Output = ()> + 'static), Error>

Create a server with graceful shutdown signal.

When the signal completes, the server will start the graceful shutdown process.

source

pub fn serve_incoming<I>(self, incoming: I) -> impl Future<Output = ()>
where I: TryStream + Send, I::Ok: AsyncRead + AsyncWrite + Send + 'static + Unpin, I::Error: Into<Box<dyn StdError + Send + Sync>>,

Setup this Server with a specific stream of incoming connections.

This can be used for Unix Domain Sockets, or TLS, etc.

Returns a Future that can be executed on the current runtime.

source

pub fn serve_incoming_with_graceful_shutdown<I>( self, incoming: I, signal: impl Future<Output = ()> + Send + 'static, ) -> impl Future<Output = ()>
where I: TryStream + Send, I::Ok: AsyncRead + AsyncWrite + Send + 'static + Unpin, I::Error: Into<Box<dyn StdError + Send + Sync>>,

Setup this Server with a specific stream of incoming connections and a signal to initiate graceful shutdown.

This can be used for Unix Domain Sockets, or TLS, etc.

When the signal completes, the server will start the graceful shutdown process.

Returns a Future that can be executed on the current runtime.

Trait Implementations§

source§

impl<F: Debug> Debug for Server<F>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<F> Freeze for Server<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for Server<F>
where F: RefUnwindSafe,

§

impl<F> Send for Server<F>
where F: Send,

§

impl<F> Sync for Server<F>
where F: Sync,

§

impl<F> Unpin for Server<F>
where F: Unpin,

§

impl<F> UnwindSafe for Server<F>
where F: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T