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 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) -> Self
Available on non-WebAssembly only.
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<H: Handler<()>>(self, handler: H) -> Self
pub fn route<H: Handler<()>>(self, handler: H) -> Self
Register a typed handler (18.28.0): a function with extractor
arguments annotated #[get("/path")] & co., whose method and pattern
come from the attribute. A router has no state, so State<S> and
&S arguments aren’t available here — use
AppWithState::route.
#[get("/users/{id}")]
fn user(Path(id): Path<u64>) -> String { format!("user {id}") }
let router = Router::new().route(user);Sourcepub fn route_conflicts(&self) -> Vec<RouteConflict>
pub fn route_conflicts(&self) -> Vec<RouteConflict>
Routes that never run (18.28.0): registered after a route with the
same method whose pattern is equivalent (/users/:id and
/users/{uid}) or matches every path they do (/users/:id before
/users/me). Routes are tried in order, so the earlier one always
wins. Each is also printed once to stderr when it is registered.
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.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Router
impl !UnwindSafe for Router
impl Freeze for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more