pub fn serve<L, M, S, B>(listener: L, make_service: M) -> Serve<L, M, S, B>where
L: Listener,
M: for<'a> Service<IncomingStream<'a, L>, Error = !, Response = S>,
S: Service<Request<Body>, Response = Response<B>, Error = !> + Clone + Send + 'static,
<S as Service<Request<Body>>>::Future: Send,
B: Body + Send + 'static,
<B as Body>::Data: Send,
<B as Body>::Error: Into<Box<dyn Error + Send + Sync>>,Expand description
Serve the service with the supplied listener.
This method of running a service exposes only some configuration knobs: how connection
tasks are spawned (via Serve::with_executor) and how long connections live (via
Serve::connection_lifetime_limits). Everything else uses hyper’s default configuration
(including timeouts); use hyper or hyper-util if you need more control.
TCP_NODELAY is set on connections accepted from a [tokio::net::TcpListener]. Use
ListenerExt::tap_io to change that.
It supports both HTTP/1 as well as HTTP/2.
§Examples
Serving a Router:
use axum::{Router, routing::get};
let router = Router::new().route("/", get(|| async { "Hello, World!" }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, router).await;See also Router::into_make_service_with_connect_info.
Serving a MethodRouter:
use axum::routing::get;
let router = get(|| async { "Hello, World!" });
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, router).await;See also MethodRouter::into_make_service_with_connect_info.
Serving a Handler:
use axum::handler::HandlerWithoutStateExt;
async fn handler() -> &'static str {
"Hello, World!"
}
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, handler.into_make_service()).await;See also HandlerWithoutStateExt::into_make_service_with_connect_info and
HandlerService::into_make_service_with_connect_info.
§Return Value
Although this future resolves to io::Result<()>, it will never actually complete or return an
error. Errors on the TCP socket will be handled by sleeping for a short while (currently, one
second).