pub struct RouteDef {
pub method: Method,
pub path: String,
pub handler: Endpoint,
pub meta: RouteMeta,
}Expand description
One route: a method, a path, a Handler and its metadata.
The path is relative to the controller (/{id}), with {param} and
{*rest} captures (see routing). A path starting with
! is absolute: it is mounted as written, without the API prefix,
version or controller prefix (!/csrf-cookie, !/health/live).
Every route must declare its access (public,
authenticated, roles,
permissions or can); the app
refuses to boot otherwise.
use http::Method;
use navree_http::RouteDef;
let route = RouteDef::on(Method::GET, "/{id}", || async { "deal" })
.name("deals.show")
.can("read", "Deal", Some("id"));
assert!(route.meta.access.is_declared());Fields§
§method: MethodThe HTTP method the route answers.
path: StringThe path relative to the controller, or absolute when it starts with !.
handler: EndpointThe handler, ready to mount.
meta: RouteMetaAccess declaration, guards, limits and documentation.
Implementations§
Source§impl RouteDef
impl RouteDef
Sourcepub fn on<H, T>(method: Method, path: impl Into<String>, handler: H) -> Selfwhere
H: Handler<T>,
T: 'static,
pub fn on<H, T>(method: Method, path: impl Into<String>, handler: H) -> Selfwhere
H: Handler<T>,
T: 'static,
A route answering method on path with handler, with no access
declaration yet.
Sourcepub fn on_guest_or<G, TG, S, TS>(
method: Method,
path: impl Into<String>,
guest: G,
signed_in: S,
) -> Self
pub fn on_guest_or<G, TG, S, TS>( method: Method, path: impl Into<String>, guest: G, signed_in: S, ) -> Self
A route at method and path that answers a guest with guest and a request
someone is signed in on with signed_in: a public site’s home page and the app’s
own home at the same address, say. Each handler takes its own extractors, so the
signed-in one may take an extractor that refuses guests (the user, their unit of
work) and the guest one is never asked for it. The route is public.
The answer varies by Cookie and Authorization, and a signed-in reader’s answer
goes out as Cache-Control: private, no-store unless its handler kept it private
itself (private or no-store, without public or s-maxage). Vary alone is
not enough: several CDNs ignore Vary: Cookie, so a CDN or proxy in front must
also skip its cache for any request that carries the session cookie (or an
Authorization header), or the guest’s page must not be cached at the edge.
use http::Method;
use navree_http::{CurrentPrincipal, RouteDef};
let home = RouteDef::on_guest_or(
Method::GET,
"!/",
|| async { "the public site" },
|CurrentPrincipal(who): CurrentPrincipal| async move { format!("hello, {}", who.user_id) },
)
.name("home");
assert!(home.meta.access.public);Sourcepub fn with_endpoint(
method: Method,
path: impl Into<String>,
endpoint: Endpoint,
) -> Self
pub fn with_endpoint( method: Method, path: impl Into<String>, endpoint: Endpoint, ) -> Self
A route answering method with endpoint.
Sourcepub fn authenticated(self) -> Self
pub fn authenticated(self) -> Self
Any signed-in user may call the route (no further authorization).
Sourcepub fn roles(self, roles: &[&str]) -> Self
pub fn roles(self, roles: &[&str]) -> Self
Requires any of the roles (checked by the RouteAuthorizer).
Sourcepub fn permissions(self, permissions: &[&str]) -> Self
pub fn permissions(self, permissions: &[&str]) -> Self
Requires the permissions (checked by the RouteAuthorizer).
Sourcepub fn can(self, action: &str, subject: &str, from: Option<&str>) -> Self
pub fn can(self, action: &str, subject: &str, from: Option<&str>) -> Self
Requires the ability action on subject; with from, on the record
whose id is the path parameter from.
Sourcepub fn handler_name(self, handler: &str) -> Self
pub fn handler_name(self, handler: &str) -> Self
The code that handles the route, for route listings
(DealsController::index; #[controller] sets it).
Sourcepub fn throttle(self, limit: u32, per: Duration) -> Self
pub fn throttle(self, limit: u32, per: Duration) -> Self
At most limit requests per per, per user (per client IP for guests).
Sourcepub fn timeout(self, d: Duration) -> Self
pub fn timeout(self, d: Duration) -> Self
Overrides the app-wide request timeout (shorter or longer).
Sourcepub fn transactional(self) -> Self
pub fn transactional(self) -> Self
Marks the handler as transactional. Informational (route table, OpenAPI, logs): navree-db runs every request in a lazy unit of work whatever this flag says.
Sourcepub fn guard(self, guard: Arc<dyn Guard>) -> Self
pub fn guard(self, guard: Arc<dyn Guard>) -> Self
Adds a guard, run before authorization and the handler.
Sourcepub fn audit(self, action: &str) -> Self
pub fn audit(self, action: &str) -> Self
Writes a custom audit entry with this action after a 2xx response
(when an AuditSink is registered).
Sourcepub fn body_limit(self, bytes: usize) -> Self
pub fn body_limit(self, bytes: usize) -> Self
Overrides the app-wide request body limit.
Sourcepub fn no_compression(self) -> Self
pub fn no_compression(self) -> Self
Sends the route’s responses uncompressed, whatever the client accepts: for a page with a secret and reflected input in one body, which a compressed length can give away (BREACH).
Sourcepub fn page(self) -> Self
pub fn page(self) -> Self
Marks the route as a page. Its errors are pages (a 404 inside the shell, a
sign-in prompt for a guest) for every client that doesn’t ask for JSON
alone, whatever the client’s Accept says. Without a mark the Accept
header decides, and a route under http.api_prefix is JSON.
Only an app with a module that renders error pages (navree-ui’s
UiModule) changes anything.
Sourcepub fn json_only(self) -> Self
pub fn json_only(self) -> Self
Marks the route as data: its errors are always the JSON error body, for every client, even one that asks for HTML.
Sourcepub fn extension(self, name: impl Into<String>, value: impl Into<Value>) -> Self
pub fn extension(self, name: impl Into<String>, value: impl Into<Value>) -> Self
Adds an OpenAPI specification extension to the route’s operation
(name should start with x-), such as navree-flags’
x-navree-flag. navree-api renders it as a member of the operation
object; a second call with the same name replaces the value.