pub struct Server<H, S> { /* private fields */ }Expand description
An HTTP server backed by a Handler and shared application state.
Server is a small wrapper around Hyper that:
- accepts TCP connections,
- converts incoming HTTP requests into
Request, - forwards them to a user-provided
Handler, - and returns a
Response.
§Type Parameters
H: The request handlerS: Shared application state, cloned per request
Implementations§
Source§impl<S> Server<(), S>
impl<S> Server<(), S>
Sourcepub fn new(state: S) -> Server<(), S>
pub fn new(state: S) -> Server<(), S>
Creates a new server with the given shared state and no handler.
This is the entry point for building a server. A handler must be
attached later using Server::handler before the server can
be started.
§Examples
let server = Server::new(app_state)
.handler(my_handler);Sourcepub fn handler<H, I>(self, handler: H) -> Server<H, S>
pub fn handler<H, I>(self, handler: H) -> Server<H, S>
Attaches a request handler to the server.
The handler is responsible for processing a Request together with
shared state S and producing a Response.
Both the normal output and exception paths must resolve to a
Response, ensuring the server can always reply to the client.
This method consumes the server and returns a new one with the handler installed.
Source§impl<H, S> Server<H, S>
impl<H, S> Server<H, S>
Sourcepub async fn listen(self, listener: TcpListener)
pub async fn listen(self, listener: TcpListener)
Starts listening for incoming connections on the given TCP listener.
This method runs indefinitely, accepting new connections and spawning a Tokio task per connection to drive the HTTP state machine.
Each request:
- clones the shared state
S, - invokes the configured
Handler, - and converts the handler’s
crate::Flowinto aResponse.
§Concurrency
- Each connection is handled concurrently.
- The handler is shared across connections via an
Arc.
§Panics
This function does not intentionally panic, but failures to accept connections are silently ignored.