web_server

Struct HttpServer

Source
pub struct HttpServer { /* private fields */ }
Expand description

Storing basics informations about server and handlers Represents http server

Implementations§

Source§

impl HttpServer

Source

pub fn new() -> Self

Create new instance of HttpServer

Source

pub fn route( self, method: HttpMethod, path: &'static str, handler: RequestHandler, ) -> Self

Add a route for a specific path and method

Source

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

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

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

pub fn not_found(self, handler: RequestHandler) -> Self

Add a handler for 404 error

server.not_found(Box::new(|_, _| "Not found!".into()));
Source§

impl HttpServer

Source

pub fn run(&self)

Launch server on port 80

Source

pub fn close(&mut self)

Source

pub fn launch(&self, port: i32)

Lauch http server, never returns

server.launch(8080).unwrap();

Trait Implementations§

Source§

impl Default for HttpServer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.