WebServer

Struct 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 With the default amount of threads: 4.

§Examples
let server: WebServer = WebServer::new();
Source

pub fn from_threads(threads: usize) -> Self

Returns a new instance of the WebServer struct with a custom amount of threads.

§Examples
    let server: WebServer = WebServer::from_threads(4);
Source

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

Gives a static response on the path specified.

§Examples
let mut 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 mut 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 mut 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 mut 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 mode(&mut self, mode: Mode)

Changes the mode of the Webserver

§Examples
    let mut server = WebServer::new();
 
    server.mode(Mode::Debug(DebugOptions {
       show_request_body: true,
       show_response_body: true,
       show_request_query: false,
       show_middleware: true,
       show_middleware_request_changes: true
    }));
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 middleware<F: Fn(Request) -> Request + Send + Sync + 'static>( &mut self, route: &str, res: F, )

Adds a middleware that runs before the route. you can use this to modify the request before use. like removing