pub struct HttpServer { /* private fields */ }
Expand description
The almighty HTTP server.
Guide:
- Create the server
let mut server = HttpServer::new(HttpServerMode::MultiThread(HttpServerThreadPool::new(2)), "127.0.0.1:3000");
- Insert handlers
server.insert_handler(|mut req, mut res| {
res.set_status(HttpStatusStruct(200, "OK"));
res.set_body(String::from("value"), String::from("Hello World!"));
Ok(req, res)
});
- Listen
server.listen(|| {
println!("Server is listening at http://127.0.0.1:3000");
});
Implementations§
Source§impl HttpServer
impl HttpServer
pub fn new(mode: HttpServerMode, bind_adr: &str) -> Self
pub fn listen<F>(&self, cb: F)where
F: Fn(),
pub fn insert_handler<F>(&mut self, handler: F)where
F: Fn(HttpRequest, HttpResponse) -> Result<(HttpRequest, HttpResponse), (HttpRequest, HttpResponse, Box<dyn Error>)> + Send + Sync + 'static,
Sourcepub fn set_error_handler<F>(&mut self, handler: F)where
F: Fn(HttpRequest, HttpResponse, Box<dyn Error>) -> (HttpRequest, HttpResponse) + Send + Sync + 'static,
pub fn set_error_handler<F>(&mut self, handler: F)where
F: Fn(HttpRequest, HttpResponse, Box<dyn Error>) -> (HttpRequest, HttpResponse) + Send + Sync + 'static,
Custom error handling function
Example:
server.set_error_handler(|req, mut res, err| {
res.set_status(HttpStatusStruct(500, "Interal Server Error"));
res.text(format!("Unhandled exception: {:?}", err));
(req, res)
});
Trait Implementations§
Source§impl Routing for HttpServer
impl Routing for HttpServer
fn insert_router(&mut self, router: Router)
Source§impl ServeStatic for HttpServer
impl ServeStatic for HttpServer
Source§fn serve_static(&mut self, root_dir: Option<String>)
fn serve_static(&mut self, root_dir: Option<String>)
Serve files in the
root_dir
folder. Default root dir is public
.Auto Trait Implementations§
impl Freeze for HttpServer
impl !RefUnwindSafe for HttpServer
impl Send for HttpServer
impl Sync for HttpServer
impl Unpin for HttpServer
impl !UnwindSafe for HttpServer
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