pub struct Router { /* private fields */ }Implementations§
Source§impl Router
impl Router
pub fn new() -> Router
Sourcepub fn with_host(self, host: &str) -> Router
pub fn with_host(self, host: &str) -> Router
Restrict this router to requests for host: their Host header
(:authority over HTTP/2 and HTTP/3), else the TLS SNI name, ignoring
case and a port host doesn’t name. Call before registering routes.
Sourcepub fn websocket<F>(self, pattern: &str, handler: F) -> Router
pub fn websocket<F>(self, pattern: &str, handler: F) -> Router
Serve WebSocket connections at pattern: an upgrade request’s
connection goes to handler with the route’s parameters, anything
else gets 426 Upgrade Required (see
WebSocket::upgrade).
use rust_web_server::router::Router;
use rust_web_server::websocket::Message;
let router = Router::new().websocket("/rooms/:room", |mut ws, params| {
let room = params.get("room").unwrap_or("lobby").to_string();
while let Ok(Some(Message::Text(text))) = ws.recv() {
let _ = ws.send_text(&format!("{room}: {text}"));
}
});Sourcepub fn patch<F>(self, pattern: &str, handler: F) -> Router
pub fn patch<F>(self, pattern: &str, handler: F) -> Router
Register a PATCH handler for pattern.
Sourcepub fn delete<F>(self, pattern: &str, handler: F) -> Router
pub fn delete<F>(self, pattern: &str, handler: F) -> Router
Register a DELETE handler for pattern.
Sourcepub fn route_entries(&self) -> Vec<RouteInfo>
pub fn route_entries(&self) -> Vec<RouteInfo>
Return a snapshot of all registered routes as (method, pattern) pairs.
Patterns are reconstructed from parsed segments, so the output exactly
matches what was passed to .get(), .post(), etc. at registration time.
Sourcepub fn handle(
&self,
request: &Request,
connection: &ConnectionInfo,
) -> Option<Response>
pub fn handle( &self, request: &Request, connection: &ConnectionInfo, ) -> Option<Response>
Try to match request against registered routes in registration order.
Returns Some(response) on the first match, None if no route matches.
The query string is stripped before matching; only the path is used.
When .with_host() is set, this returns None immediately unless the
request’s host (Host header, else SNI) matches.