Function serve

fn serve<L, M, S>(listener: L, make_service: M) -> Serve<L, M, S>
where
    L: Listener,
    M: for<'a> Service<IncomingStream<'a, L>, Error = std::convert::Infallible, Response = S>,
    S: Service<axum_core::extract::Request, Response = axum_core::response::Response, Error = std::convert::Infallible> + Clone + Send + 'static,
    <S as >::Future: Send

Serve the service with the supplied listener.

This method of running a service is intentionally simple and doesn't support any configuration. Use hyper or hyper-util if you need configuration.

It supports both HTTP/1 as well as HTTP/2.

Examples

Serving a Router:

use axum::{Router, routing::get};

# async {
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.unwrap();
# };

See also Router::into_make_service_with_connect_info.

Serving a MethodRouter:

use axum::routing::get;

# async {
let router = get(|| async { "Hello, World!" });

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, router).await.unwrap();
# };

See also MethodRouter::into_make_service_with_connect_info.

Serving a Handler:

use axum::handler::HandlerWithoutStateExt;

# async {
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.unwrap();
# };

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).