pub struct HttpServer<A>where
A: ToSocketAddrs,{ /* private fields */ }Expand description
The struct to initialise your http server and finally listen on some port
§Example usage:
HttpServer::new(("127.0.0.1", 8080)).run() // no_op http server listening on port 8080Implementations§
Source§impl<Addr> HttpServer<Addr>
impl<Addr> HttpServer<Addr>
Sourcepub fn add_middleware(self, f: fn(req: Request) -> Request) -> Self
pub fn add_middleware(self, f: fn(req: Request) -> Request) -> Self
Initialises middleware or replaces if there was already some added
subject to change
§Example usage:
HttpServer::new(("127.0.0.1", 8080)).add_middleware(|req| {
println!("we got request: {req:#?}");
req
})Sourcepub fn route<F: HandlerFn + 'static>(
self,
path: impl Into<String>,
method: HttpMethod,
f: F,
) -> Self
pub fn route<F: HandlerFn + 'static>( self, path: impl Into<String>, method: HttpMethod, f: F, ) -> Self
Register a custom route
§Example usage:
HttpServer::new(("127.0.0.1", 8080)).route("/some_path", HttpMethod::Other("custom"), |_| {"hi"})Sourcepub fn post<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
pub fn post<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
Register a POST method
§Example usage:
fn my_post(_req: HttpRequest) -> impl Response {
// ... Super complex DB activity
"I'll keep you posted"
}
HttpServer::new(("127.0.0.1", 8080)).post("/drop/prod/db", my_post)Sourcepub fn delete<F: HandlerFn + 'static>(
self,
path: impl Into<String>,
f: F,
) -> Self
pub fn delete<F: HandlerFn + 'static>( self, path: impl Into<String>, f: F, ) -> Self
Register a DELETE method
§Example usage:
fn my_delete(_req: HttpRequest) -> impl Response {
// delete browser history ...
"Yeah I don't use the internet bro trust me..."
}
HttpServer::new(("127.0.0.1", 8080)).delete("/homework", my_delete)Sourcepub fn update<F: HandlerFn + 'static>(
self,
path: impl Into<String>,
f: F,
) -> Self
pub fn update<F: HandlerFn + 'static>( self, path: impl Into<String>, f: F, ) -> Self
Register an UPDATE method
§Example usage:
fn im_getting_tired_of_writing_these(_req: HttpRequest) -> impl Response {
// just read the others like .get() and .post() bro
"Yeah I don't use the internet bro trust me..."
}
HttpServer::new(("127.0.0.1", 8080)).delete("/homework", im_getting_tired_of_writing_these)Sourcepub fn put<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
pub fn put<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
Register a PUT method
§Example usage:
fn im_getting_tired_of_writing_these(_req: HttpRequest) -> impl Response {
"WHY THE HECK DID I ADD SO MANY OF THESE THINGS"
}
HttpServer::new(("127.0.0.1", 8080)).delete("/us-east1", im_getting_tired_of_writing_these)Sourcepub fn patch<F: HandlerFn + 'static>(
self,
path: impl Into<String>,
f: F,
) -> Self
pub fn patch<F: HandlerFn + 'static>( self, path: impl Into<String>, f: F, ) -> Self
like .post() but patch
Sourcepub fn head<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
pub fn head<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
I just took this one from hoppscotch I never heard of the head method before
read .post() and stuff for documentation
Sourcepub fn options<F: HandlerFn + 'static>(
self,
path: impl Into<String>,
f: F,
) -> Self
pub fn options<F: HandlerFn + 'static>( self, path: impl Into<String>, f: F, ) -> Self
Shoutout to chatgpt for this one: Register an OPTIONS method
This attaches a handler to the given path that responds to http OPTIONS
requests. Typically used for capability discovery, CORS preflight checks,
or politely telling browsers what they are allowed to do.
§Example usage:
fn options_method(_req: HttpRequest) -> impl Response {
""
}
HttpServer::new(("127.0.0.1", 8080)).options("/home", options_method);§Note:
OPTIONS requests are generally expected to return headers describing
allowed methods and behaviors. A response body is usually unnecessary and
often ignored, but nothing is stopping you from adding one if you enjoy
disappointing strict HTTP purists.