Struct salvo_core::Server
source · 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::Conn: Transport + Send + Unpin + 'static,
L::Error: Into<Box<dyn StdError + Send + Sync + 'static>>,
impl<L> Server<L>where
L: Listener,
L::Conn: Transport + Send + Unpin + 'static,
L::Error: Into<Box<dyn StdError + Send + Sync + 'static>>,
sourcepub async fn try_serve<S>(self, service: S) -> Result<(), Error>where
S: Into<Service>,
pub async fn try_serve<S>(self, service: S) -> 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)where
S: Into<Service>,
G: Future<Output = ()> + Send + 'static,
pub async fn serve_with_graceful_shutdown<S, G>(self, addr: S, signal: G)where
S: Into<Service>,
G: Future<Output = ()> + Send + 'static,
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(());
}