pub struct Route<I, R> { /* private fields */ }Expand description
A route: a path pattern, one handler per method, and who may use it.
Patterns are made of literal segments, parameters (:id, one segment)
and one wildcard at the end (*rest, the rest of the path). Literals are
written decoded (/café); web-net compares them with the canonical
request path.
use web_net::{App, get, Route};
let app = App::builder()
.route(get("/", |cx| { cx.html(200, "<h1>Home</h1>"); Ok(()) }).public())
.route(
Route::at("/contact")
.get(|cx| { cx.html(200, "<form method=post></form>"); Ok(()) })
.post(|cx| { cx.see_other("/contact/thanks") })
.public()
// A real form gets CSRF protection: Builder::csrf and a gate
// whose identities carry a binding.
.csrf_exempt("example: the POST changes nothing"),
)
.build()?;A closure that reads fields of a custom identity types its parameter,
because the closure is checked before the builder fixes the type (a
named fn handler needs nothing extra):
use web_net::{Cx, Route, get};
struct User { name: String }
impl web_net::Identity for User {
fn is_guest(&self) -> bool { self.name.is_empty() }
}
enum Rule { SignedIn }
let account: Route<User, Rule> = get("/account", |cx: &mut Cx<'_, User>| {
let name = cx.identity().name.clone();
cx.text(200, &name);
Ok(())
})
.allow(Rule::SignedIn);Implementations§
Source§impl<I: 'static, R> Route<I, R>
impl<I: 'static, R> Route<I, R>
Sourcepub fn on(self, method: Method, handler: impl Handler<I>) -> Self
pub fn on(self, method: Method, handler: impl Handler<I>) -> Self
Adds a handler for method. HEAD follows GET unless it has its
own; OPTIONS is answered for every route unless it has its own.
Sourcepub fn public(self) -> Self
pub fn public(self) -> Self
Anyone may use the route, guests included. Public is a declaration,
never a default: a route with neither public nor allow stops the
build.
Sourcepub fn allow(self, rule: R) -> Self
pub fn allow(self, rule: R) -> Self
Those rule allows may use the route; the gate’s authorize decides
on every request.
Sourcepub fn work(self, work: WorkClass) -> Self
pub fn work(self, work: WorkClass) -> Self
How the handlers behave: Quick (the default), Io, or Cpu. A
dispatcher in front of web-net reads it through
App::classify to move slow work off the
connection’s worker.
Sourcepub fn csrf_exempt(self, reason: &'static str) -> Self
pub fn csrf_exempt(self, reason: &'static str) -> Self
Takes the route out of CSRF protection, with the reason (a webhook signed by its sender, an API authenticated by a bearer token that a browser never sends on its own). The reason is required and must not be empty: an exemption is a decision, never a default.
Sourcepub fn form_limits(self, limits: Limits) -> Self
pub fn form_limits(self, limits: Limits) -> Self
The budgets for reading this route’s forms (field count, sizes, files;
default form_net::Limits::DEFAULT). Keep them to what the form
needs: they bound what one request can make the server hold.
Sourcepub fn cors(self, cors: Cors) -> Self
pub fn cors(self, cors: Cors) -> Self
The route’s CORS policy: preflights answered, replies to allowed origins readable by their scripts.
Sourcepub fn cache(self, policy: CachePolicy) -> Self
pub fn cache(self, policy: CachePolicy) -> Self
How the route’s pages may be cached.
Sourcepub fn template(self, name: &'static str) -> Self
pub fn template(self, name: &'static str) -> Self
The template Cx::render uses. Resolved when the
application is built: an unknown name stops the build.