pub struct HttpServer { /* private fields */ }Expand description
The struct to initialise your http server and finally listen on some port
§Example usage:
use torus_http::server::HttpServer;
HttpServer::new().listen(("127.0.0.1", 8080)); // no_op http server listening on port 8080Implementations§
Source§impl HttpServer
impl HttpServer
Sourcepub fn add_middleware(self, f: fn(req: HttpRequest) -> HttpRequest) -> Self
pub fn add_middleware(self, f: fn(req: HttpRequest) -> HttpRequest) -> Self
Initialises middleware or replaces if there was already some added
subject to change
§Example usage:
use torus_http::server::HttpServer;
HttpServer::new().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:
use torus_http::server::HttpServer;
use torus_http::method::HttpMethod;
HttpServer::new().route("/some_path", HttpMethod::other("custom"), |_| {"hi"});Sourcepub fn get<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
pub fn get<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self
Register a GET method
§Example usage:
use torus_http::server::HttpServer;
use torus_http::request::HttpRequest;
use torus_http::response::Response;
fn home_method(_req: HttpRequest) -> impl Response {
"hello, world"
}
HttpServer::new().get("/home", home_method);§Note:
I drop the body for get requests as that is apparently standard
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:
use torus_http::server::HttpServer;
use torus_http::request::HttpRequest;
use torus_http::response::Response;
fn my_post(_req: HttpRequest) -> impl Response {
// ... Super complex DB activity
"I'll keep you posted"
}
HttpServer::new().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:
use torus_http::server::HttpServer;
use torus_http::request::HttpRequest;
use torus_http::response::Response;
fn my_delete(_req: HttpRequest) -> impl Response {
// delete browser history ...
"Yeah I don't use the internet bro trust me..."
}
HttpServer::new().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:
use torus_http::server::HttpServer;
use torus_http::request::HttpRequest;
use torus_http::response::Response;
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().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:
use torus_http::request::HttpRequest;
use torus_http::server::HttpServer;
use torus_http::response::Response;
fn im_getting_tired_of_writing_these(_req: HttpRequest) -> impl Response {
"WHY THE HECK DID I ADD SO MANY OF THESE THINGS"
}
HttpServer::new().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:
use torus_http::server::HttpServer;
use torus_http::request::HttpRequest;
use torus_http::response::Response;
fn options_method(_req: HttpRequest) -> impl Response {
""
}
HttpServer::new().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.
pub fn set_state<T: Send + Sync + 'static>(self, state: T) -> Self
Sourcepub fn listen(self, address: impl ToSocketAddrs) -> Result<(), ServerError>
pub fn listen(self, address: impl ToSocketAddrs) -> Result<(), ServerError>
Start your http server
§Errors
- Failed binding listener to address
- Failed reading the stream to the buffer
- Failed getting the stream
- Failed parsing the request
- Failed flushing to the stream