Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
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

Source

pub fn new() -> Self

Create a builder with defaults.

Source

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

Source

pub fn bind_socket(self, addr: SocketAddr) -> Self

Set the bind address as a parsed SocketAddr.

Source

pub fn route<H, T>(self, path: &str, handler: H) -> Self
where H: Handler<T, ()>, T: 'static,

Register a route, delegating to axum::Router::route.

Source

pub fn merge(self, other: Router) -> Self

Merge another router (delegates to axum::Router::merge).

Source

pub fn nest(self, prefix: &str, router: Router) -> Self

Mount a router at a nested path (delegates to axum::Router::nest).

Source

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.

Source

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.

Source

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

Source

pub fn disable_tracing(self) -> Self

Disable the default tracing middleware.

Source

pub fn disable_request_id(self) -> Self

Disable the default request-id middleware.

Source

pub fn enable_cors(self) -> Self

Enable CORS with permissive defaults (CorsLayer::permissive()).

Source

pub fn enable_cors_with(self, layer: CorsLayer) -> Self

Enable CORS with a custom CorsLayer.

Source

pub fn enable_compression(self) -> Self

Enable response compression (gzip/br/zstd).

Source

pub fn health_path(self, path: &str) -> Self

Customise the health endpoint path. Default /health.

Source

pub fn health_response<F, R>(self, responder: F) -> Self
where F: Fn() -> R + Send + Sync + 'static, R: IntoResponse + 'static,

Provide a custom responder for the health endpoint.

Source

pub fn disable_health(self) -> Self

Disable the built-in health endpoint.

Source

pub async fn build(self) -> Result<Server>

Bind the listener and build a Server ready to run.

Trait Implementations§

Source§

impl Default for ServerBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ServiceExt for T

Source§

fn compression(self) -> Compression<Self>
where Self: Sized,

Compresses response bodies. Read more
Source§

fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using HTTP status codes. Read more
Source§

fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using gRPC headers. Read more
Source§

fn set_request_id<M>( self, header_name: HeaderName, make_request_id: M, ) -> SetRequestId<Self, M>
where Self: Sized, M: MakeRequestId,

Add request id header and extension. Read more
Source§

fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>
where Self: Sized, M: MakeRequestId,

Add request id header and extension, using x-request-id as the header name. Read more
Source§

fn propagate_request_id( self, header_name: HeaderName, ) -> PropagateRequestId<Self>
where Self: Sized,

Propgate request ids from requests to responses. Read more
Source§

fn propagate_x_request_id(self) -> PropagateRequestId<Self>
where Self: Sized,

Propgate request ids from requests to responses, using x-request-id as the header name. Read more
Source§

fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>
where Self: Sized,

Intercept requests with over-sized payloads and convert them into 413 Payload Too Large responses. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more