pub struct HttpServer { /* private fields */ }
Expand description
Storing basics informations about server and handlers Represents http server
Implementations§
Source§impl HttpServer
impl HttpServer
Sourcepub fn route(
self,
method: HttpMethod,
path: &'static str,
handler: RequestHandler,
) -> Self
pub fn route( self, method: HttpMethod, path: &'static str, handler: RequestHandler, ) -> Self
Add a route for a specific path and method
Sourcepub fn get(self, path: &'static str, handler: RequestHandler) -> Self
pub fn get(self, path: &'static str, handler: RequestHandler) -> Self
Add a route for GET method and a specific path
server.get("/user/:id", Box::new(|request, _| {
database.get_user(request.params.get("id").unwrap()).into()
}))
Sourcepub fn post(self, path: &'static str, handler: RequestHandler) -> Self
pub fn post(self, path: &'static str, handler: RequestHandler) -> Self
Add a route for POST method and a specific path
server.post("/add-user", Box::new(|request, mut default_response| {
println!("Save new user!\n\n{}", request.get_body());
default_response.headers.insert("Access-Control-Allow-Origin", "*");
default_repsonse
}))
Sourcepub fn any(self, path: &'static str, handler: RequestHandler) -> Self
pub fn any(self, path: &'static str, handler: RequestHandler) -> Self
Add a route for a specific path and any method
server
.get("/endpoint", Box::new(|request, default_response| "Gate GET were obtained".into()))
.post("/endpoint", Box::new(|request, default_response| "Gate POST were obtained".into()))
.any("/endpoint", Box::new(|request, default_response| "Another gate were obtained".into()))
Sourcepub fn not_found(self, handler: RequestHandler) -> Self
pub fn not_found(self, handler: RequestHandler) -> Self
Add a handler for 404 error
server.not_found(Box::new(|_, _| "Not found!".into()));
Trait Implementations§
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