Struct shs::Server

source ·
pub struct Server<E: Debug + Display> { /* private fields */ }
Expand description

HTTP 1.1 server.

Example usage:

use anyhow::Error;
use fehler::throws;
use shs::{Request, Server};

#[throws]
fn handler(req: &mut Request) {
    todo!();
}

let mut server = Server::new("127.0.0.1:1234")?;
server.route("GET /hello", &handler)?;
server.launch()?;

Implementations§

source§

impl<E: Debug + Display + 'static> Server<E>

source

pub fn new(address: &str) -> Result<Server<E>, Error>

Create a new Server.

source

pub fn route( &mut self, route: &str, handler: &'static Handler<E> ) -> Result<(), Error>

Add a new route. The basic format is "METHOD /path". The path can contain parameters that start with a colon, for example "/resource/:key"; these parameters act as wild cards that can match any single path segment.

source

pub fn set_error_handler(&mut self, error_handler: &'static ErrorHandler<E>)

Set a custom error handler.

The default error handler:

  • Logs the error
  • If the error is NotFound, sets the status to NotFound and the body to “not found”
  • If the error is Custom, sets the status to InternalServerError and the body to “internal server error”
source

pub fn launch(self) -> Result<(), Error>

Start the server.

source

pub fn test_request( &self, input: &TestRequest ) -> Result<TestResponse, RequestError<E>>

Send a fake request for testing.

Auto Trait Implementations§

§

impl<E> Freeze for Server<E>

§

impl<E> RefUnwindSafe for Server<E>

§

impl<E> Send for Server<E>

§

impl<E> Sync for Server<E>

§

impl<E> Unpin for Server<E>

§

impl<E> UnwindSafe for Server<E>

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.