pub fn serve<M, S>(tcp_listener: TcpListener, make_service: M) -> Serve<M, S>where
M: for<'a> Service<IncomingStream<'a>, Error = Infallible, Response = S>,
S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Expand description
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};
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;
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 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
.