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 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 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 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 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 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 listen(self, addr: &str) -> Result<(), ()>
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();sourcepub fn HttpRequest(request: Request) -> Result<String, ErrorMessage>
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§
impl !RefUnwindSafe for WebServer
impl Send for WebServer
impl Sync for WebServer
impl Unpin for WebServer
impl !UnwindSafe for WebServer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more