Struct spike::Server

source ·
pub struct Server<'a> { /* private fields */ }
Expand description

A listening HTTP server that accepts HTTP 1 connections.

Implementations§

source§

impl<'a> Server<'a>

source

pub fn builder() -> ServerBuilder

Starts the ServerBuilder.

source

pub fn bind<A>(addr: A) -> Server<'static>where A: ToSocketAddrs,

Binds the Server to the given addr.

Panics

This method will panic if binding to the address fails. For a non panic method to bind the server, see ServerBuilder::try_bind.

source

pub fn serve<S>(self, service: S) -> Result<(), Error>where S: Service + Send + Clone + 'static,

Serves an Service on a thread per connection model, backed by a thread pool.

Example
Server::bind("0.0.0.0:4444").serve(|req: Request<_>| {
    Response::builder()
        .status(StatusCode::OK)
        .body(req.into_body())
})
source

pub fn serve_single_thread<S>(self, service: S) -> Result<(), Error>where S: Service + Clone,

Serves an Service on a single thread. This is useful when your Service is not Send. Note that if a connection is kept alive on this mode, no other request may be served before the said connection is closed.

Example
Server::bind("0.0.0.0:4444").serve_single_thread(|req: Request<_>| {
    Response::builder()
        .status(StatusCode::OK)
        .body(req.into_body())
})
source

pub fn make_service<M>(self, make_service: M) -> Result<(), Error>where M: MakeService + Clone + 'static, <M as MakeService>::Service: Send,

Serves an Connection. This should be used when you need to execute some logic on every connection.

Example
Server::builder()
    .bind("0.0.0.0:4444")
    .make_service(|conn: &Connection| {
        println!("New connection arrived: {:?}", conn.peer_addr());

        Ok::<_, Infallible>(|_req| {
            Response::builder()
                .status(StatusCode::OK)
                .body(())
        })
    })

Trait Implementations§

source§

impl From<TcpListener> for Server<'static>

source§

fn from(listener: TcpListener) -> Server<'static>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Server<'a>

§

impl<'a> !Send for Server<'a>

§

impl<'a> !Sync for Server<'a>

§

impl<'a> Unpin for Server<'a>

§

impl<'a> !UnwindSafe for Server<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.