Struct snowboard::Server

source ·
pub struct Server { /* private fields */ }
Expand description

Simple server struct

Implementations§

source§

impl Server

source

pub fn new(addr: &str) -> Self

Create a new server instance. The server will not start until the run method is called. The addr parameter is a string in the format of host:port.

Example

See the basic example in examples/basic.

source

pub fn set_buffer_size(&mut self, new_size: usize) -> &mut Self

Sest the buffer size. The buffer size is used to read incoming requests. The default buffer size is 8KB (8192 bytes).

source

pub fn on_request(&mut self, handler: Handler) -> &mut Self

Set the handler function. This function will be called when a request is received. If no handler is set, the server will respond with a 503 Service Unavailable.

Example
use snowboard::Server;

let server = Server::new("localhost:8080")
    .on_request(|request| {
        println!("{:?}", request);

        snowboard::response!(ok, "Hello, world!")
    });

// server.run();
source

pub fn add_middleware(&mut self, handler: Middleware) -> &mut Self

Add a middleware function. Middleware functions are called before the handler function. They can be used to modify the request or to return a response. If a response is returned, the handler function will not be called. If multiple middleware functions are added, they will be called in the order they were added.

Example
use snowboard::Server;

let mut server = Server::new("localhost:8080");

server.add_middleware(|mut request| {
   request.set_header("X-Server", "Snowboard");

   // Request, Response
   (request, None)
});
Example 2
use snowboard::Server;

let mut server = Server::new("localhost:8080");

server.add_middleware(|request| {
   // Request, Response
  (request, Some(snowboard::response!(ok, "Hello, world!")))
});
source

pub fn run(&self) -> !

Start the server.

Auto Trait Implementations§

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

§

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

§

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.