pub struct WebServer { /* private fields */ }Expand description
A WebServer that can be hosted with the ‘listen’ method
Implementations§
Source§impl WebServer
impl WebServer
Sourcepub fn new() -> Self
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();Sourcepub fn from_threads(threads: usize) -> Self
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);Sourcepub fn on_static(&mut self, req_type: RequestType, route: &str, res: &str)
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>" // TrueSourcepub fn on<F: Fn(Request) -> (String, ResponseCode) + Send + Sync + 'static>(
&mut self,
req_type: RequestType,
route: &str,
res: F,
)
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();Sourcepub fn on_all<F: Fn(Request) -> (String, ResponseCode) + Send + Sync + 'static>(
&mut self,
req_type: RequestType,
route: &str,
res: F,
)
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”
Sourcepub fn not_found(&mut self, html: &str)
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>" // TrueNormaly WebServer has a default response:
404 NOT FOUND
Sourcepub fn mode(&mut self, mode: Mode)
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
}));