pub struct Group<I, R, E: Render = NoRender> { /* private fields */ }Expand description
Routes sharing a path prefix and an access boundary.
A group’s rule holds for every route in it: a route’s own rule adds to it (both are checked, the group’s first), and a route declared public inside a group with a rule stops the build. A route that declares nothing takes the group’s access.
use web_net::{App, Group, get};
let app = App::builder()
.group(
Group::at("/docs")
.route(get("/", |cx| cx.text(200, "index")))
.route(get("/:page", |cx| cx.text(200, "page"))),
)
.build()?;Implementations§
Source§impl<I: 'static, R, E: Render> Group<I, R, E>
impl<I: 'static, R, E: Render> Group<I, R, E>
Sourcepub fn at(prefix: impl Into<String>) -> Self
pub fn at(prefix: impl Into<String>) -> Self
A group under prefix (/admin).
use web_net::{App, Group, get};
let app = App::builder()
.group(Group::at("/admin").route(get("/", |cx| cx.text(200, "dashboard"))))
.build()?;
let r = serve_net::testing::respond(&app, b"GET /admin HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "dashboard");Sourcepub fn cors(self, cors: Cors) -> Self
pub fn cors(self, cors: Cors) -> Self
The CORS policy of routes that declare none.
use web_net::{App, Cors, Group, get};
let api = Group::at("/api")
.cors(Cors::new().origin("https://app.example.com"))
.route(get("/items", |cx| cx.bytes(200, "application/json", b"[]")));
let app = App::builder().group(api).build()?;
let r = serve_net::testing::respond(
&app,
b"GET /api/items HTTP/1.1\r\nHost: localhost\r\nOrigin: https://app.example.com\r\n\r\n",
);
assert_eq!(r.header("access-control-allow-origin"), Some(&b"https://app.example.com"[..]));Sourcepub fn cache(self, policy: CachePolicy) -> Self
pub fn cache(self, policy: CachePolicy) -> Self
The cache policy of routes that declare none.
use web_net::{App, CachePolicy, Group, get};
let docs = Group::at("/docs")
.cache(CachePolicy::Guests { max_age: 60 })
.route(get("/intro", |cx| cx.text(200, "intro")));
let app = App::builder().group(docs).build()?;
let r = serve_net::testing::respond(&app, b"GET /docs/intro HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.header("cache-control"), Some(&b"public, max-age=60"[..]));Sourcepub fn public(self) -> Self
pub fn public(self) -> Self
Routes that declare nothing are public.
use web_net::{App, Group, get};
// `NoStaff` is a gate that denies `Rule::Staff` to everyone.
let app = App::with_gate(NoStaff)
.group(Group::at("/docs").public().route(get("/intro", |cx| cx.text(200, "intro"))))
.build()?;
let r = serve_net::testing::respond(&app, b"GET /docs/intro HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.status, 200);Sourcepub fn allow(self, rule: R) -> Self
pub fn allow(self, rule: R) -> Self
Every route in the group needs rule, as well as its own.
use web_net::{App, Group, get};
// `NoStaff` is a gate that denies `Rule::Staff` to everyone.
let app = App::with_gate(NoStaff)
.group(Group::at("/admin").allow(Rule::Staff).route(get("/users", |cx| cx.text(200, "users"))))
.build()?;
let r = serve_net::testing::respond(&app, b"GET /admin/users HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.status, 403);Sourcepub fn route(self, route: Route<I, R, E>) -> Self
pub fn route(self, route: Route<I, R, E>) -> Self
Adds a route; its pattern is joined to the prefix.
use web_net::{App, Group, get};
let blog = Group::at("/blog").route(get("/:slug", |cx| {
let slug: String = cx.param_as("slug")?;
cx.text(200, &slug)
}));
let app = App::builder().group(blog).build()?;
let r = serve_net::testing::respond(&app, b"GET /blog/hello HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "hello");Sourcepub fn group(self, group: Group<I, R, E>) -> Self
pub fn group(self, group: Group<I, R, E>) -> Self
Adds a nested group; its prefix is joined to this one, and its access holds inside this group’s (both rules apply).
use web_net::{App, Group, get};
let v1 = Group::at("/v1").route(get("/items", |cx| cx.text(200, "items")));
let app = App::builder().group(Group::at("/api").group(v1)).build()?;
let r = serve_net::testing::respond(&app, b"GET /api/v1/items HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "items");Trait Implementations§
Auto Trait Implementations§
impl<I, R, E = NoRender> !RefUnwindSafe for Group<I, R, E>
impl<I, R, E = NoRender> !UnwindSafe for Group<I, R, E>
impl<I, R, E> Forget for Group<I, R, E>
impl<I, R, E> Freeze for Group<I, R, E>
impl<I, R, E> Move for Group<I, R, E>
impl<I, R, E> Send for Group<I, R, E>
impl<I, R, E> Sync for Group<I, R, E>
impl<I, R, E> Unpin for Group<I, R, E>
impl<I, R, E> UnsafeUnpin for Group<I, R, E>
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