pub struct Server<I, S, E = Exec> {
incoming: I,
make_service: S,
protocol: Http<E>,
}
Expand description
A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default.
Server
is a Future
mapping a bound listener with a set of service
handlers. It is built using the Builder
, and the future
completes when the server has been shutdown. It should be run by an
Executor
.
Fields§
§incoming: I
§make_service: S
§protocol: Http<E>
Implementations§
source§impl Server<AddrIncoming, ()>
impl Server<AddrIncoming, ()>
sourcepub fn bind(addr: &SocketAddr) -> Builder<AddrIncoming>
pub fn bind(addr: &SocketAddr) -> Builder<AddrIncoming>
sourcepub fn try_bind(addr: &SocketAddr) -> Result<Builder<AddrIncoming>>
pub fn try_bind(addr: &SocketAddr) -> Result<Builder<AddrIncoming>>
Tries to bind to the provided address, and returns a Builder
.
sourcepub fn from_tcp(
listener: StdTcpListener,
) -> Result<Builder<AddrIncoming>, Error>
pub fn from_tcp( listener: StdTcpListener, ) -> Result<Builder<AddrIncoming>, Error>
Create a new instance from a std::net::TcpListener
instance.
source§impl<S, E> Server<AddrIncoming, S, E>
impl<S, E> Server<AddrIncoming, S, E>
sourcepub fn local_addr(&self) -> SocketAddr
pub fn local_addr(&self) -> SocketAddr
Returns the local address that this server is bound to.
source§impl<I, IO, IE, S, E, B> Server<I, S, E>where
I: Accept<Conn = IO, Error = IE>,
IE: Into<Box<dyn StdError + Send + Sync>>,
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
S: MakeServiceRef<IO, Body, ResBody = B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: HttpBody + 'static,
B::Error: Into<Box<dyn StdError + Send + Sync>>,
E: ConnStreamExec<<S::Service as HttpService<Body>>::Future, B>,
impl<I, IO, IE, S, E, B> Server<I, S, E>where
I: Accept<Conn = IO, Error = IE>,
IE: Into<Box<dyn StdError + Send + Sync>>,
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
S: MakeServiceRef<IO, Body, ResBody = B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: HttpBody + 'static,
B::Error: Into<Box<dyn StdError + Send + Sync>>,
E: ConnStreamExec<<S::Service as HttpService<Body>>::Future, B>,
sourcepub fn with_graceful_shutdown<F>(self, signal: F) -> Graceful<I, S, F, E> ⓘ
pub fn with_graceful_shutdown<F>(self, signal: F) -> Graceful<I, S, F, E> ⓘ
Prepares a server to handle graceful shutdown when the provided future completes.
§Example
// Make a server from the previous examples...
let server = Server::bind(&([127, 0, 0, 1], 3000).into())
.serve(make_service);
// Prepare some signal for when the server should start shutting down...
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
let graceful = server
.with_graceful_shutdown(async {
rx.await.ok();
});
// Await the `server` receiving the signal...
if let Err(e) = graceful.await {
eprintln!("server error: {}", e);
}
// And later, trigger the signal by calling `tx.send(())`.
let _ = tx.send(());
fn poll_next_( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Result<Connecting<IO, S::Future, E>>>>
pub(super) fn poll_watch<W>( self: Pin<&mut Self>, cx: &mut Context<'_>, watcher: &W, ) -> Poll<Result<()>>
Trait Implementations§
source§impl<I, IO, IE, S, B, E> Future for Server<I, S, E>where
I: Accept<Conn = IO, Error = IE>,
IE: Into<Box<dyn StdError + Send + Sync>>,
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
S: MakeServiceRef<IO, Body, ResBody = B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: HttpBody + 'static,
B::Error: Into<Box<dyn StdError + Send + Sync>>,
E: ConnStreamExec<<S::Service as HttpService<Body>>::Future, B> + NewSvcExec<IO, S::Future, S::Service, E, NoopWatcher>,
impl<I, IO, IE, S, B, E> Future for Server<I, S, E>where
I: Accept<Conn = IO, Error = IE>,
IE: Into<Box<dyn StdError + Send + Sync>>,
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
S: MakeServiceRef<IO, Body, ResBody = B>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: HttpBody + 'static,
B::Error: Into<Box<dyn StdError + Send + Sync>>,
E: ConnStreamExec<<S::Service as HttpService<Body>>::Future, B> + NewSvcExec<IO, S::Future, S::Service, E, NoopWatcher>,
impl<'__pin, I, S, E> Unpin for Server<I, S, E>where
PinnedFieldsOf<__Origin<'__pin, I, S, E>>: Unpin,
Auto Trait Implementations§
impl<I, S, E> Freeze for Server<I, S, E>
impl<I, S, E> RefUnwindSafe for Server<I, S, E>
impl<I, S, E> Send for Server<I, S, E>
impl<I, S, E> Sync for Server<I, S, E>
impl<I, S, E> UnwindSafe for Server<I, S, E>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Map this future’s output to a different type, returning a new future of
the resulting type. Read more
source§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
Map this future’s output to a different type, returning a new future of
the resulting type. Read more
source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
Chain on a computation for when a future finished, passing the result of
the future to the provided closure
f
. Read moresource§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
source§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Convert this future into a single element stream. Read more
source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Flatten the execution of this future when the output of this
future is itself another future. Read more
source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Flatten the execution of this future when the successful result of this
future is a stream. Read more
source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
Fuse a future such that
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read moresource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Do something with the output of a future before passing it on. Read more
source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Catches unwinding panics while polling the future. Read more
Create a cloneable handle to this future where all handles will resolve
to the same result. Read more
source§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
Turn this future into a future that yields
()
on completion and sends
its output to another future on a separate task. Read moresource§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Wrap the future in a Box, pinning it. Read more
source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
Wrap the future in a Box, pinning it. Read more
source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Turns a
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Turns a
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more
source§impl<Fut> TryFutureExt for Fut
impl<Fut> TryFutureExt for Fut
source§fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>
fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>
source§fn map_ok<T, F>(self, f: F) -> MapOk<Self, F>
fn map_ok<T, F>(self, f: F) -> MapOk<Self, F>
Maps this future’s success value to a different value. Read more
source§fn map_ok_or_else<T, E, F>(self, e: E, f: F) -> MapOkOrElse<Self, F, E>
fn map_ok_or_else<T, E, F>(self, e: E, f: F) -> MapOkOrElse<Self, F, E>
Maps this future’s success value to a different value, and permits for error handling resulting in the same type. Read more
source§fn map_err<E, F>(self, f: F) -> MapErr<Self, F>
fn map_err<E, F>(self, f: F) -> MapErr<Self, F>
Maps this future’s error value to a different value. Read more
source§fn and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F>
fn and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F>
Executes another future after this one resolves successfully. The
success value is passed to a closure to create this subsequent future. Read more
source§fn or_else<Fut, F>(self, f: F) -> OrElse<Self, Fut, F>
fn or_else<Fut, F>(self, f: F) -> OrElse<Self, Fut, F>
Executes another future if this one resolves to an error. The
error value is passed to a closure to create this subsequent future. Read more
source§fn inspect_ok<F>(self, f: F) -> InspectOk<Self, F>
fn inspect_ok<F>(self, f: F) -> InspectOk<Self, F>
Do something with the success value of a future before passing it on. Read more
source§fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
Do something with the error value of a future before passing it on. Read more
source§fn try_flatten(self) -> TryFlatten<Self, Self::Ok>
fn try_flatten(self) -> TryFlatten<Self, Self::Ok>
Flatten the execution of this future when the successful result of this
future is another future. Read more
source§fn try_flatten_stream(self) -> TryFlattenStream<Self>
fn try_flatten_stream(self) -> TryFlattenStream<Self>
Flatten the execution of this future when the successful result of this
future is a stream. Read more