pub struct Router { /* private fields */ }Implementations§
Source§impl Router
impl Router
pub fn new() -> Self
Sourcepub fn with_host(self, host: &str) -> Self
pub fn with_host(self, host: &str) -> Self
Restrict this router to requests whose SNI hostname (TLS) or Host
header (plain HTTP) matches host. Call before registering routes.
Sourcepub fn websocket<F>(self, pattern: &str, handler: F) -> Self
pub fn websocket<F>(self, pattern: &str, handler: F) -> Self
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 delete<F>(self, pattern: &str, handler: F) -> Self
pub fn delete<F>(self, pattern: &str, handler: F) -> Self
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 SNI hostname (TLS) or Host header (plain HTTP) matches.