Struct mini_server::HTTPServer

source ·
pub struct HTTPServer<'a> {
    pub addr: &'static str,
    pub port: u32,
    /* private fields */
}
Expand description

This struct provide a simple http server that handle many of the use cases

§Example

use mini_server::*;

let mut app = http_server!("localhost", 4221);

app.get("/", |_, _| {
    let mut response = HTTPResponse::default();
    response.set_body(b"Hello World!".to_vec());

    response
});

§Path

The path is an expression that can contains dynamic variables.

  • Basic paths: /, /this/is/a/path, …
  • Dynamic path: /this/is/a/@varibale, /this/is/another/#variable

# and @ are prefixes for dynamic values. # for denoting numbers and @ for strings

Fields§

§addr: &'static str§port: u32

Implementations§

source§

impl<'a> HTTPServer<'a>

source

pub fn new(addr: &'static str, port: u32) -> Self

source

pub fn on_ready<T>(&mut self, handler: T)
where T: Fn() + 'a,

source

pub fn on_shutdown<T>(&mut self, handler: T)
where T: Fn() + 'a,

source

pub fn connect<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn delete<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn get<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn head<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn options<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn patch<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn post<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn put<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn trace<T>(&mut self, path: &'static str, handler: T)
where T: Fn(HTTPRequest, &PathMap) -> HTTPResponse + 'a,

source

pub fn on_any<T>(&mut self, handler: T)
where T: Fn(&HTTPRequest, &mut HTTPResponse) + 'a,

Trait Implementations§

source§

impl Server<&mut TcpStream, HTTPRequest> for HTTPServer<'_>

source§

fn handle_request(&self, stream: &mut TcpStream, req: HTTPRequest)

source§

fn run(&self)

Auto Trait Implementations§

§

impl<'a> Freeze for HTTPServer<'a>

§

impl<'a> !RefUnwindSafe for HTTPServer<'a>

§

impl<'a> !Send for HTTPServer<'a>

§

impl<'a> !Sync for HTTPServer<'a>

§

impl<'a> Unpin for HTTPServer<'a>

§

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>,

§

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>,

§

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.