Skip to main content

RouteDef

Struct RouteDef 

Source
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: Method

The HTTP method the route answers.

§path: String

The path relative to the controller, or absolute when it starts with !.

§handler: Endpoint

The handler, ready to mount.

§meta: RouteMeta

Access declaration, guards, limits and documentation.

Implementations§

Source§

impl RouteDef

Source

pub fn on<H, T>(method: Method, path: impl Into<String>, handler: H) -> Self
where H: Handler<T>, T: 'static,

A route answering method on path with handler, with no access declaration yet.

Source

pub fn on_guest_or<G, TG, S, TS>( method: Method, path: impl Into<String>, guest: G, signed_in: S, ) -> Self
where G: Handler<TG>, TG: 'static, S: Handler<TS>, TS: 'static,

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);
Source

pub fn with_endpoint( method: Method, path: impl Into<String>, endpoint: Endpoint, ) -> Self

A route answering method with endpoint.

Source

pub fn public(self) -> Self

No authentication or authorization needed.

Source

pub fn authenticated(self) -> Self

Any signed-in user may call the route (no further authorization).

Source

pub fn roles(self, roles: &[&str]) -> Self

Requires any of the roles (checked by the RouteAuthorizer).

Source

pub fn permissions(self, permissions: &[&str]) -> Self

Requires the permissions (checked by the RouteAuthorizer).

Source

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.

Source

pub fn name(self, name: &str) -> Self

The route name (e.g. deals.show).

Source

pub fn handler_name(self, handler: &str) -> Self

The code that handles the route, for route listings (DealsController::index; #[controller] sets it).

Source

pub fn throttle(self, limit: u32, per: Duration) -> Self

At most limit requests per per, per user (per client IP for guests).

Source

pub fn timeout(self, d: Duration) -> Self

Overrides the app-wide request timeout (shorter or longer).

Source

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.

Source

pub fn guard(self, guard: Arc<dyn Guard>) -> Self

Adds a guard, run before authorization and the handler.

Source

pub fn api(self, meta: ApiMeta) -> Self

OpenAPI metadata.

Source

pub fn audit(self, action: &str) -> Self

Writes a custom audit entry with this action after a 2xx response (when an AuditSink is registered).

Source

pub fn body_limit(self, bytes: usize) -> Self

Overrides the app-wide request body limit.

Source

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).

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for RouteDef

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RouteDef

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more