Struct salvo::Server [−][src]
pub struct Server { /* fields omitted */ }
Implementations
Set custom catchers for server.
Set allowed media types for server, any media type is not include in this list will not allowed to send to client.
Bind to a socket address, returning a Future
that can be
executed on any runtime.
Panics
Panics if we are unable to bind to the provided address.
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.
pub async fn bind_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr> + 'static,
signal: impl Future<Output = ()> + Send + 'static
)
pub async fn bind_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr> + 'static,
signal: impl Future<Output = ()> + Send + '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
any runtime.
Example
use salvo_core::prelude::*;
use tokio::sync::oneshot;
#[fn_handler]
async fn hello_world(res: &mut Response) {
res.render_plain_text("Hello World!");
}
#[tokio::main]
async fn main() {
let (tx, rx) = oneshot::channel();
let router = Router::new().get(hello_world);
let server = Server::new(router).bind_with_graceful_shutdown(([0, 0, 0, 0], 3131), async {
rx.await.ok();
});
// Spawn the server into a runtime
tokio::task::spawn(server);
// Later, start the shutdown...
let _ = tx.send(());
}
pub async fn try_bind_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr> + 'static,
signal: impl Future<Output = ()> + Send + 'static
) -> Result<SocketAddr, Error>
pub async fn try_bind_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr> + 'static,
signal: impl Future<Output = ()> + Send + 'static
) -> Result<SocketAddr, Error>
Create a server with graceful shutdown signal.
When the signal completes, the server will start the graceful shutdown process.
pub async fn bind_incoming<I>(self, incoming: I) where
I: TryStream + Send,
<I as TryStream>::Ok: 'static,
<I as TryStream>::Ok: AsyncRead,
<I as TryStream>::Ok: AsyncWrite,
<I as TryStream>::Ok: Send,
<I as TryStream>::Ok: Unpin,
<I as TryStream>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
pub async fn bind_incoming<I>(self, incoming: I) where
I: TryStream + Send,
<I as TryStream>::Ok: 'static,
<I as TryStream>::Ok: AsyncRead,
<I as TryStream>::Ok: AsyncWrite,
<I as TryStream>::Ok: Send,
<I as TryStream>::Ok: Unpin,
<I as TryStream>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
Bind to a stream, returning a Future
that can be
executed on any runtime.
Panics
Panics if we are unable to bind to the provided address.
pub async fn try_bind_incoming<I>(self, incoming: I) -> Result<(), Error> where
I: TryStream + Send,
<I as TryStream>::Ok: 'static,
<I as TryStream>::Ok: AsyncRead,
<I as TryStream>::Ok: AsyncWrite,
<I as TryStream>::Ok: Send,
<I as TryStream>::Ok: Unpin,
<I as TryStream>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
pub async fn try_bind_incoming<I>(self, incoming: I) -> Result<(), Error> where
I: TryStream + Send,
<I as TryStream>::Ok: 'static,
<I as TryStream>::Ok: AsyncRead,
<I as TryStream>::Ok: AsyncWrite,
<I as TryStream>::Ok: Send,
<I as TryStream>::Ok: Unpin,
<I as TryStream>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
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.
pub async fn bind_incoming_with_graceful_shutdown<I>(
self,
incoming: I,
signal: impl Future<Output = ()> + Send + 'static
) where
I: TryStream + Send,
<I as TryStream>::Ok: 'static,
<I as TryStream>::Ok: AsyncRead,
<I as TryStream>::Ok: AsyncWrite,
<I as TryStream>::Ok: Send,
<I as TryStream>::Ok: Unpin,
<I as TryStream>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
pub async fn try_bind_incoming_with_graceful_shutdown<I>(
self,
incoming: I,
signal: impl Future<Output = ()> + Send + 'static
) -> Result<(), Error> where
I: TryStream + Send,
<I as TryStream>::Ok: 'static,
<I as TryStream>::Ok: AsyncRead,
<I as TryStream>::Ok: AsyncWrite,
<I as TryStream>::Ok: Send,
<I as TryStream>::Ok: Unpin,
<I as TryStream>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
pub async fn try_bind_incoming_with_graceful_shutdown<I>(
self,
incoming: I,
signal: impl Future<Output = ()> + Send + 'static
) -> Result<(), Error> where
I: TryStream + Send,
<I as TryStream>::Ok: 'static,
<I as TryStream>::Ok: AsyncRead,
<I as TryStream>::Ok: AsyncWrite,
<I as TryStream>::Ok: Send,
<I as TryStream>::Ok: Unpin,
<I as TryStream>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
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 any runtime.
Auto Trait Implementations
impl !RefUnwindSafe for Server
impl !UnwindSafe for Server
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more