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 typeMounted: type-level list of mounted sub-APIs (starts asMNil)Provided: type-level list of provided effects (starts asENil)
Implementations§
Source§impl<A: ApiSpec, M, P> ServerBuilder<A, M, P>
impl<A: ApiSpec, M, P> ServerBuilder<A, M, P>
Sourcepub fn mount<Sub: ApiSpec, H: Serves<Sub>>(
self,
handlers: H,
) -> ServerBuilder<A, MCons<Sub, M>, P>
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.
Sourcepub fn provide<E: Effect>(self) -> ServerBuilder<A, M, ECons<E, P>>
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.
Sourcepub fn layer<L>(self, layer: L) -> LayeredServerBuilder<A, M, P, L::Service>
pub fn layer<L>(self, layer: L) -> LayeredServerBuilder<A, M, P, L::Service>
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.
Sourcepub fn with_state<T: Clone + Send + Sync + 'static>(self, state: T) -> Self
pub fn with_state<T: Clone + Send + Sync + 'static>(self, state: T) -> Self
Set shared state accessible via State<T> extractors.
Sourcepub fn max_body_size(self, max: usize) -> Self
pub fn max_body_size(self, max: usize) -> Self
Set the maximum request body size in bytes.
Sourcepub fn build<MIdx, PIdx>(self) -> Server<A>where
A: AllMounted<M, MIdx> + AllProvided<P, PIdx>,
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