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>
impl<E: Debug + Display + 'static> Server<E>
sourcepub fn route(
&mut self,
route: &str,
handler: &'static Handler<E>
) -> Result<(), Error>
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.
sourcepub fn set_error_handler(&mut self, error_handler: &'static ErrorHandler<E>)
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”
sourcepub fn test_request(
&self,
input: &TestRequest
) -> Result<TestResponse, RequestError<E>>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more