Skip to main content

Server

Struct Server 

Source
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 handler
  • S: Shared application state, cloned per request

Implementations§

Source§

impl<S> Server<(), S>

Source

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);
Source

pub fn handler<H, I>(self, handler: H) -> Server<H, S>
where H: Handler<I, S, Output = Response, Exception = Response>,

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>

Source

pub async fn listen(self, listener: TcpListener)
where H: Handler<Request, S, Output = Response> + Send + Sync + 'static, H::Future: Send + 'static, S: Clone + Send + 'static,

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:

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

Auto Trait Implementations§

§

impl<H, S> Freeze for Server<H, S>
where H: Freeze, S: Freeze,

§

impl<H, S> RefUnwindSafe for Server<H, S>

§

impl<H, S> Send for Server<H, S>
where H: Send, S: Send,

§

impl<H, S> Sync for Server<H, S>
where H: Sync, S: Sync,

§

impl<H, S> Unpin for Server<H, S>
where H: Unpin, S: Unpin,

§

impl<H, S> UnsafeUnpin for Server<H, S>
where H: UnsafeUnpin, S: UnsafeUnpin,

§

impl<H, S> UnwindSafe for Server<H, S>
where H: UnwindSafe, S: UnwindSafe,

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