HttpServer

Struct HttpServer 

Source
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 8080

Implementations§

Source§

impl<Addr> HttpServer<Addr>
where Addr: ToSocketAddrs + Clone + Send + Sync + 'static,

Source

pub fn new(address: Addr) -> Self

Initialise an http server on an address

Source

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
})
Source

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"})
Source

pub fn get<F: HandlerFn + 'static>(self, path: impl Into<String>, f: F) -> Self

Register a GET method

§Example usage:
fn home_method(_req: HttpRequest) -> impl Response {
    "hello, world"
}
HttpServer::new(("127.0.0.1", 8080)).get("/home", )
§Note:

I drop the body for get requests as that is apparently standard

Source

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)
Source

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)
Source

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)
Source

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)
Source

pub fn patch<F: HandlerFn + 'static>( self, path: impl Into<String>, f: F, ) -> Self

like .post() but patch

Source

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

Source

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.

Source

pub fn run(self) -> 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

Auto Trait Implementations§

§

impl<A> Freeze for HttpServer<A>
where A: Freeze,

§

impl<A> !RefUnwindSafe for HttpServer<A>

§

impl<A> Send for HttpServer<A>
where A: Send,

§

impl<A> Sync for HttpServer<A>
where A: Sync,

§

impl<A> Unpin for HttpServer<A>
where A: Unpin,

§

impl<A> !UnwindSafe for HttpServer<A>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.