pub struct Server<L> { /* private fields */ }
Expand description
HTTP Server
A Server
is created to listen on a port, parse HTTP requests, and hand them off to a Service
.
Implementations§
source§impl<L> Server<L>where
L: Listener,
<L as Accept>::Conn: 'static + Transport + Send + Unpin,
<L as Accept>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
impl<L> Server<L>where
L: Listener,
<L as Accept>::Conn: 'static + Transport + Send + Unpin,
<L as Accept>::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
sourcepub async fn serve<S>(self, service: S) -> impl Future<Output = ()>where
S: Into<Service>,
pub async fn serve<S>(self, service: S) -> impl Future<Output = ()>where
S: Into<Service>,
Serve a Service
sourcepub async fn try_serve<S>(
self,
service: S
) -> impl Future<Output = Result<(), Error>>where
S: Into<Service>,
pub async fn try_serve<S>(
self,
service: S
) -> impl Future<Output = Result<(), Error>>where
S: Into<Service>,
Try to serve a Service
sourcepub async fn serve_with_graceful_shutdown<S, G>(
self,
addr: S,
signal: G
) -> impl Future<Output = ()>where
S: Into<Service>,
G: 'static + Future<Output = ()> + Send,
pub async fn serve_with_graceful_shutdown<S, G>(
self,
addr: S,
signal: G
) -> impl Future<Output = ()>where
S: Into<Service>,
G: 'static + Future<Output = ()> + Send,
Serve with graceful shutdown signal.
Example
use salvo_core::prelude::*;
#[handler]
async fn hello_world(res: &mut Response) {
res.render("Hello World!");
}
#[tokio::main]
async fn main() {
let (tx, rx) = oneshot::channel();
let router = Router::new().get(hello_world);
let server = Server::new(TcpListener::bind("127.0.0.1:7878")).serve_with_graceful_shutdown(router, async {
rx.await.ok();
});
// Spawn the server into a runtime
tokio::task::spawn(server);
// Later, start the shutdown...
let _ = tx.send(());
}