pub struct Server { /* private fields */ }Expand description
The HTTP server that owns and dispatches a collection of SocketType routes.
Build routes through the ServerMechanism builder chain, register each with
mechanism, then start the server with serve.
§Example
let mut server = Server::default();
server
.mechanism(
ServerMechanism::get("/ping")
.onconnect(|| async { reply!(json => Pong { ok: true }) })
)
.mechanism(
ServerMechanism::delete("/session")
.onconnect(|| async { reply!() })
);
// Blocks forever — call only to actually run the server:
// server.serve(([0, 0, 0, 0], 8080)).await;§Caution
Calling serve with no routes registered will panic.
Implementations§
Source§impl Server
impl Server
Sourcepub fn mechanism(&mut self, mech: SocketType) -> &mut Self
pub fn mechanism(&mut self, mech: SocketType) -> &mut Self
Registers a SocketType route on this server.
Routes are evaluated in registration order. Returns &mut Self for chaining.
Sourcepub fn serve(self, addr: impl Into<SocketAddr>) -> ServerFuture
pub fn serve(self, addr: impl Into<SocketAddr>) -> ServerFuture
Binds to addr and starts serving all registered routes.
Returns a ServerFuture that can be:
.await’d — runs the server in the current task (infinite loop).background()’d — spawns the server as a Tokio background task
§Panics
Panics if no routes have been registered or if the address cannot be bound.
Sourcepub fn serve_with_graceful_shutdown(
self,
addr: impl Into<SocketAddr>,
shutdown: impl Future<Output = ()> + Send + 'static,
) -> ServerFuture
pub fn serve_with_graceful_shutdown( self, addr: impl Into<SocketAddr>, shutdown: impl Future<Output = ()> + Send + 'static, ) -> ServerFuture
Binds to addr, serves all registered routes, and shuts down gracefully when
shutdown resolves.
Returns a ServerFuture that can be .await’d or .background()’d.
§Example
use tokio::sync::oneshot;
let (tx, rx) = oneshot::channel::<()>();
let handle = server.serve_with_graceful_shutdown(
([127, 0, 0, 1], 8080),
async move { rx.await.ok(); },
).background();
tx.send(()).ok();
handle.await.ok();Sourcepub fn serve_from_listener(
self,
listener: TcpListener,
shutdown: impl Future<Output = ()> + Send + 'static,
) -> ServerFuture
pub fn serve_from_listener( self, listener: TcpListener, shutdown: impl Future<Output = ()> + Send + 'static, ) -> ServerFuture
Serves all registered routes from an already-bound listener, shutting down
gracefully when shutdown resolves.
Returns a ServerFuture that can be .await’d or .background()’d.
Use this when port 0 is passed to TcpListener::bind and you need to know
the actual OS-assigned port before the server starts.
§Example
use tokio::net::TcpListener;
use tokio::sync::oneshot;
let listener = TcpListener::bind("127.0.0.1:0").await?;
let port = listener.local_addr()?.port();
let (tx, rx) = oneshot::channel::<()>();
let handle = server
.serve_from_listener(listener, async move { rx.await.ok(); })
.background();
tx.send(()).ok();
handle.await.ok();Sourcepub fn rebind(&mut self, addr: impl Into<SocketAddr>) -> &mut Self
pub fn rebind(&mut self, addr: impl Into<SocketAddr>) -> &mut Self
Stores addr as this server’s default bind address.
This is a pre-serve convenience setter. Call it before
serve_managed or any other serve* variant to
record the initial address without starting the server.
Returns &mut Self for method chaining.
Sourcepub fn serve_managed(self, addr: impl Into<SocketAddr>) -> BackgroundServer
pub fn serve_managed(self, addr: impl Into<SocketAddr>) -> BackgroundServer
Starts all registered routes in a background Tokio task and returns a
BackgroundServer handle.
Unlike serve* + .background(), this method keeps a live route table
inside the handle, enabling:
BackgroundServer::rebind— graceful stop + restart on a new addressBackgroundServer::mechanism— add routes without restartingBackgroundServer::addr— query the current bind addressBackgroundServer::stop— shut down and await completion
§Panics
Panics if no routes have been registered.
§Example
let mut server = Server::default();
server.mechanism(
toolkit_zero::socket::server::ServerMechanism::get("/ping")
.onconnect(|| async { reply!(json => Pong { ok: true }) })
);
let mut bg = server.serve_managed(([127, 0, 0, 1], 8080));
println!("Running on {}", bg.addr());
bg.rebind(([127, 0, 0, 1], 9090)).await;
println!("Rebound to {}", bg.addr());
bg.stop().await;