pub struct Routes(/* private fields */);Expand description
Which routes a hook runs for.
use web_net::Routes;
let every = Routes::all();
let blog = Routes::prefix("/blog");
let pages = Routes::named(&["home", "about"]);Implementations§
Source§impl Routes
impl Routes
Sourcepub fn all() -> Self
pub fn all() -> Self
Every route.
use web_net::{App, Hook, Routes, get};
let app = App::builder()
.hook(Hook::Finish, Routes::all(), |cx| {
cx.set_header("x-site", "demo");
Ok(())
})
.route(get("/", |cx| cx.text(200, "home")))
.build()?;
let r = serve_net::testing::respond(&app, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-site"), Some(&b"demo"[..]));Sourcepub fn prefix(prefix: impl Into<String>) -> Self
pub fn prefix(prefix: impl Into<String>) -> Self
Routes whose pattern starts with prefix (/blog).
use serve_net::testing::respond;
use web_net::{App, Hook, Routes, get};
let app = App::builder()
.hook(Hook::Finish, Routes::prefix("/docs"), |cx| {
cx.set_header("x-section", "docs");
Ok(())
})
.route(get("/docs/intro", |cx| cx.text(200, "intro")))
.route(get("/docsearch", |cx| cx.text(200, "search")))
.build()?;
let r = respond(&app, b"GET /docs/intro HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-section"), Some(&b"docs"[..]));
// A prefix matches whole segments.
let r = respond(&app, b"GET /docsearch HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-section"), None);Sourcepub fn named(names: &[&'static str]) -> Self
pub fn named(names: &[&'static str]) -> Self
Routes with one of these names.
use web_net::{App, Hook, Routes, get};
let app = App::builder()
.hook(Hook::Finish, Routes::named(&["home", "about"]), |cx| {
cx.set_header("x-page", "static");
Ok(())
})
.route(get("/", |cx| cx.text(200, "home")).name("home"))
.build()?;
let r = serve_net::testing::respond(&app, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("x-page"), Some(&b"static"[..]));Trait Implementations§
Auto Trait Implementations§
impl Forget for Routes
impl Freeze for Routes
impl Move for Routes
impl RefUnwindSafe for Routes
impl Send for Routes
impl Sync for Routes
impl Unpin for Routes
impl UnsafeUnpin for Routes
impl UnwindSafe for Routes
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