pub struct Server { /* private fields */ }Expand description
Represents an HTTP server.
This server listens for incoming HTTP requests, dispatches them to the correct handler based on the method and path, and sends back appropriate HTTP responses. It supports GET and POST routes.
The server is multi-threaded, handling each incoming connection in a new thread.
Implementations§
Source§impl Server
impl Server
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates and initializes a new server instance.
§Returns
A new instance of Server with an empty route configuration.
Sourcepub fn get<F>(&mut self, path: &str, handler: F)
pub fn get<F>(&mut self, path: &str, handler: F)
Registers a GET route with a specified path and handler.
§Parameters
- ‘path’ : The route path to register, e.g., ‘/home’.
- ‘handler’ : The closure that processes the request for this path.
Sourcepub fn post<F>(&mut self, path: &str, handler: F)
pub fn post<F>(&mut self, path: &str, handler: F)
Registers a POST route with a specified path and handler.
§Parameters
- ‘path’ : The route path to register, e.g., ‘/submit’.
- ‘handler’ : The closure that processes the request for this path.
§Example
use guest_server::{Server,HttpResponse};
let mut server = Server::new();
server.post("/submit",submit);
fn submit(body: Option<Vec<u8>>) -> HttpResponse {
HttpResponse::new(200, Some("{\"key\":\"value\"}".to_string())).insert_header("Content-Type","application/json")
}
server.listener(8080);Auto Trait Implementations§
impl Freeze for Server
impl RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl UnwindSafe for Server
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