Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
pub struct ServerBuilder<A: ApiSpec, Mounted = MNil, Provided = ENil> { /* private fields */ }
Expand description

A builder for composing large APIs from sub-APIs.

Each .mount::<SubAPI>(handlers) call registers a sub-API’s handlers and records the mount at the type level. .build() only compiles when all sub-APIs in the full API type have been mounted.

§Example

ServerBuilder::<FullAPI>::new()
    .mount::<UsersAPI>(users_handlers)
    .mount::<OrdersAPI>(orders_handlers)
    .build()
    .serve(addr)
    .await?;

A builder for composing large APIs from sub-APIs with compile-time tracking of both mounted sub-APIs and provided middleware effects.

  • A: the full API type
  • Mounted: type-level list of mounted sub-APIs (starts as MNil)
  • Provided: type-level list of provided effects (starts as ENil)

Implementations§

Source§

impl<A: ApiSpec> ServerBuilder<A, MNil, ENil>

Source

pub fn new() -> Self

Create a new builder for the given API type.

Source§

impl<A: ApiSpec, M, P> ServerBuilder<A, M, P>

Source

pub fn mount<Sub: ApiSpec, H: Serves<Sub>>( self, handlers: H, ) -> ServerBuilder<A, MCons<Sub, M>, P>

Mount a sub-API with its handler tuple.

The type system tracks which sub-APIs have been mounted.

Source

pub fn provide<E: Effect>(self) -> ServerBuilder<A, M, ECons<E, P>>

Declare that a middleware effect has been provided.

Pair with .layer() to apply the actual middleware. The type system tracks which effects have been provided across all sub-APIs.

Source

pub fn layer<L>(self, layer: L) -> LayeredServerBuilder<A, M, P, L::Service>
where L: Layer<RouterService>, L::Service: Service<Request<Incoming>, Response = Response<BoxBody>, Error = Infallible> + Clone + Send + 'static, <L::Service as Service<Request<Incoming>>>::Future: Send + 'static,

Apply a Tower middleware layer to the server.

Typically paired with .provide::<E>() to discharge an effect requirement. The layer wraps the entire router — applied once, covering all mounted sub-APIs.

Source

pub fn with_state<T: Clone + Send + Sync + 'static>(self, state: T) -> Self

Set shared state accessible via State<T> extractors.

Source

pub fn max_body_size(self, max: usize) -> Self

Set the maximum request body size in bytes.

Source

pub fn build<MIdx, PIdx>(self) -> Server<A>
where A: AllMounted<M, MIdx> + AllProvided<P, PIdx>,

Finalize the server.

Only compiles when:

  • All sub-APIs in the full API type have been mounted
  • All Requires<E, _> effects have been provided

Trait Implementations§

Source§

impl<A: ApiSpec> Default for ServerBuilder<A, MNil, ENil>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<A, Mounted = MNil, Provided = ENil> !Freeze for ServerBuilder<A, Mounted, Provided>

§

impl<A, Mounted = MNil, Provided = ENil> !RefUnwindSafe for ServerBuilder<A, Mounted, Provided>

§

impl<A, Mounted, Provided> Send for ServerBuilder<A, Mounted, Provided>
where A: Send, Mounted: Send, Provided: Send,

§

impl<A, Mounted, Provided> Sync for ServerBuilder<A, Mounted, Provided>
where A: Sync, Mounted: Sync, Provided: Sync,

§

impl<A, Mounted, Provided> Unpin for ServerBuilder<A, Mounted, Provided>
where A: Unpin, Mounted: Unpin, Provided: Unpin,

§

impl<A, Mounted, Provided> UnsafeUnpin for ServerBuilder<A, Mounted, Provided>

§

impl<A, Mounted = MNil, Provided = ENil> !UnwindSafe for ServerBuilder<A, Mounted, Provided>

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<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<B, C, R, H> Serves<VersionedApi<B, C, R>> for H
where R: ApiSpec, H: Serves<R>,

Source§

fn register(self, router: &mut Router)

Register all handlers into the router.
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
Source§

impl<T> IsEndpoint<T> for T