pub struct ServerBuilder { /* private fields */ }Expand description
Typed builder for Server.
Construct via Server::builder.
§Defaults
- bind address:
0.0.0.0:8080 - request timeout: 30s (applied via
tower_http::timeout::TimeoutLayer) - request body limit: 2 MiB (via
Self::request_body_limit) - tracing, request-id, health endpoint at
/health: enabled - CORS, compression, shutdown timeout: disabled / unset
Implementations§
Source§impl ServerBuilder
impl ServerBuilder
Sourcepub fn bind_addr(self, addr: impl Into<String>) -> Self
pub fn bind_addr(self, addr: impl Into<String>) -> Self
Set the bind address as a string (e.g. "0.0.0.0:3000", "[::]:8080").
Sourcepub fn bind_socket(self, addr: SocketAddr) -> Self
pub fn bind_socket(self, addr: SocketAddr) -> Self
Set the bind address as a parsed SocketAddr.
Sourcepub fn route<H, T>(self, path: &str, handler: H) -> Self
pub fn route<H, T>(self, path: &str, handler: H) -> Self
Register a route, delegating to axum::Router::route.
Sourcepub fn merge(self, other: Router) -> Self
pub fn merge(self, other: Router) -> Self
Merge another router (delegates to axum::Router::merge).
Sourcepub fn nest(self, prefix: &str, router: Router) -> Self
pub fn nest(self, prefix: &str, router: Router) -> Self
Mount a router at a nested path (delegates to axum::Router::nest).
Sourcepub fn request_timeout(self, d: Duration) -> Self
pub fn request_timeout(self, d: Duration) -> Self
Set the per-request timeout. Default 30s.
The timeout wraps the entire request, including all middleware (tracing, CORS, custom layers) and the handler itself. Slow middleware will count against this deadline.
Sourcepub fn request_body_limit(self, bytes: usize) -> Self
pub fn request_body_limit(self, bytes: usize) -> Self
Cap the size of incoming request bodies (default 2 MiB).
Requests with bodies larger than this receive an immediate
413 Payload Too Large response without buffering the full body.
Mitigates slow-drip and body-bomb attacks against public-facing
servers.
Sourcepub fn shutdown_timeout(self, d: Duration) -> Self
pub fn shutdown_timeout(self, d: Duration) -> Self
Bound the graceful shutdown drain (default: unbounded).
After the shutdown future resolves, axum stops accepting new
connections and waits for in-flight requests to finish. Without
a bound, a stuck handler keeps the server alive forever. Set this
to enforce a deadline; in-flight requests still running after the
deadline will be dropped and run_with_shutdown returns
Err(Error::ShutdownTimeout).
Sourcepub fn disable_tracing(self) -> Self
pub fn disable_tracing(self) -> Self
Disable the default tracing middleware.
Sourcepub fn disable_request_id(self) -> Self
pub fn disable_request_id(self) -> Self
Disable the default request-id middleware.
Sourcepub fn enable_cors(self) -> Self
pub fn enable_cors(self) -> Self
Enable CORS with permissive defaults (CorsLayer::permissive()).
Sourcepub fn enable_cors_with(self, layer: CorsLayer) -> Self
pub fn enable_cors_with(self, layer: CorsLayer) -> Self
Enable CORS with a custom CorsLayer.
Sourcepub fn enable_compression(self) -> Self
pub fn enable_compression(self) -> Self
Enable response compression (gzip/br/zstd).
Sourcepub fn health_path(self, path: &str) -> Self
pub fn health_path(self, path: &str) -> Self
Customise the health endpoint path. Default /health.
Sourcepub fn health_response<F, R>(self, responder: F) -> Self
pub fn health_response<F, R>(self, responder: F) -> Self
Provide a custom responder for the health endpoint.
Sourcepub fn disable_health(self) -> Self
pub fn disable_health(self) -> Self
Disable the built-in health endpoint.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for ServerBuilder
impl !RefUnwindSafe for ServerBuilder
impl !UnwindSafe for ServerBuilder
impl Send for ServerBuilder
impl Sync for ServerBuilder
impl Unpin for ServerBuilder
impl UnsafeUnpin for ServerBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn compression(self) -> Compression<Self>where
Self: Sized,
fn compression(self) -> Compression<Self>where
Self: Sized,
Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
x-request-id as the header name. Read moreSource§fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
413 Payload Too Large responses. Read more