Struct netserver::WebServer

source ·
pub struct WebServer { /* private fields */ }
Expand description

A WebServer that can be hosted with the ‘listen’ method

Implementations§

source§

impl WebServer

source

pub fn new() -> Self

Returns a new instance of the WebServer struct.

Examples
let server: WebServer = WebServer::new();
source

pub fn on_static(&mut self, req_type: RequestType, route: &str, res: &str)

Gives a static response on the path specified.

Examples
let server = WebServer::new();
 
server.on_static(RequestType::Get, "/api", "<h1>Hello</h1>")
 
server.listen("127.0.0.1:5000").unwrap();

JavaScript

 let res = await fetch("/api");
 let json = await res.json();
 
 json == "<h1>Hello</h1>" // True
source

pub fn on<F: Fn(Request) -> (String, ResponseCode) + Send + Sync + 'static>( &mut self, req_type: RequestType, route: &str, res: F )

Runs on the route given and request type.

Examples
let server = WebServer::new();
 
server.on(RequestType::Get, "/api", |req| {
    (String::from("<h1>Welcome to my API!</h1>"), ResponseCode::Rc200)
})
 
server.listen("127.0.0.1:5000").unwrap();
source

pub fn on_all<F: Fn(Request) -> (String, ResponseCode) + Send + Sync + 'static>( &mut self, req_type: RequestType, route: &str, res: F )

Runs on all routes that start with the given string and request type. the more nested the route is the higher priority it has. the ‘on’ method has the highest priority.

Examples
let server = WebServer::new();
 
server.on_all(RequestType::Get, "/api", |req| {
    (String::from("<h1>Welcome to my API!</h1>"), ResponseCode::Rc200)
})
 
server.listen("127.0.0.1:5000").unwrap();

So, if i make a get request on path “/api/id” it would still be as if i just said “/api”

source

pub fn not_found(&mut self, html: &str)

Changes the default response for 404 errors to the new.

Examples
let server = WebServer::new();
 
server.not_found("<h1>404 Not found</h1>")
 
server.listen("127.0.0.1:5000").unwrap();

JavaScript

 let res = await fetch("/api/foo");
 let json = await res.json();
 
 json == "<h1>404 Not found</h1>" // True

Normaly WebServer has a default response:

404 NOT FOUND

source

pub fn listen(self, addr: &str) -> Result<(), ()>

Hosts Webserver on given address.

Examples
let server = WebServer::new();
 
server.listen("127.0.0.1:5000").unwrap();
source

pub fn HttpRequest(request: Request) -> Result<String, ErrorMessage>

Makes a request with the given Request struct.

It does not use the query field, Instead it uses the path.

Examples
  let response: String = WebServer::HttpRequest(Request {}).unwrap();

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.