pub struct Mount { /* private fields */ }Expand description
Any tower service under a path prefix, inside the app’s pipeline.
A module declares it with
m.mount(..). It answers the prefix
itself and every path below it, for every method unless
methods names some, behind the pipeline a route has:
request id, request context and client address, the app’s global layers
(session and CSRF among them), authentication, then on every request the
mount’s access check, guards, throttle, timeout and body limit. One of
public, authenticated,
roles, permissions or
can is required, as for a route: a mount without an access
declaration fails the boot, and so does one whose prefix is malformed,
starts with /_ (the framework’s own paths), or overlaps a route or
another mount.
The service sees the request as the app received it but for its URI:
scheme and authority as received, and for the path / followed by the
rest of the path below the prefix as written (/ for the prefix itself),
then ? and the query as written. Nothing is decoded, so %2F, %3F and
%23 reach the service as they were sent. Before that the router has
refused a . or .. segment (raw, encoded, or once the rest of the path
is decoded), a backslash, a NUL and bytes that are not UTF-8; the mount
refuses an empty segment below the prefix (//, also once %2F is read
as /) with 400. /khata/ and the prefix in another case are not found.
A service that decodes its own path still treats it as untrusted input.
A request with an Upgrade header (a WebSocket or h2c handshake) or the
method CONNECT never reaches the service: a mounted service answers
requests, it does not take over the connection. One that reaches the mount
is answered 501 after the access check. With methods,
a method the list leaves out, CONNECT among them, is the router’s 405
before the access check, as for a route; HEAD runs the GET answer when
GET is listed, and OPTIONS is a 204 with Allow.
The body is cut off at the mount’s limit also when it comes without a
length. A panic in the service answers 500, as in a handler. An
error of the service answers 500 with the error as its source, which the
app’s error mappings (m.map_error) see and the log records; one that
comes from the body’s limit or the server’s read timeout answers 413 or
408, as the body extractors do. A response below 500 the service writes
goes out as written, a 4xx included: its status, headers and body are the
service’s (a 422 with a plain-text body stays a 422 with that body). A 5xx
with no body or a short plain-text one (of known length, up to 16 KiB)
takes the app’s JSON error shape, its text logged, never sent; any other
5xx body (JSON, a longer or streamed text) goes out as written. The
pipeline adds X-Request-Id, and the default security headers where the
service set none.
The service is built in register; it reaches the app’s providers through
the AppContext in each request’s extensions.
use std::time::Duration;
use navree_http::extract::Request;
use navree_http::{AppRouter, ModuleBuilderMountExt, Mount};
use navree_kernel::{Module, ModuleBuilder};
struct LedgerModule;
impl Module for LedgerModule {
fn register(&self, m: &mut ModuleBuilder) {
// The service of another crate; here, one that answers every path.
let khata = AppRouter::from_handler(|request: Request| async move {
format!("the khata at {}", request.uri().path())
});
m.mount(
Mount::new("/khata", khata)
.authenticated()
.throttle(60, Duration::from_secs(60)),
);
}
}Implementations§
Source§impl Mount
impl Mount
Sourcepub fn new<S, B>(prefix: &str, service: S) -> Mount
pub fn new<S, B>(prefix: &str, service: S) -> Mount
service at prefix (/khata, absolute: not under
http.api_prefix): it sees requests for prefix and prefix/... with
the prefix taken off the path, raw (/khata/v1/items%3Fx arrives as
/v1/items%3Fx). Its access is declared next.
Sourcepub fn public(self) -> Mount
pub fn public(self) -> Mount
No authentication or authorization: every request reaches the service.
Sourcepub fn authenticated(self) -> Mount
pub fn authenticated(self) -> Mount
Any signed-in user may call the mount (no further authorization).
Sourcepub fn roles(self, roles: &[&str]) -> Mount
pub fn roles(self, roles: &[&str]) -> Mount
Requires any of the roles (checked by the
RouteAuthorizer on every request).
Sourcepub fn permissions(self, permissions: &[&str]) -> Mount
pub fn permissions(self, permissions: &[&str]) -> Mount
Requires the permissions (checked by the
RouteAuthorizer on every request).
Sourcepub fn can(self, action: &str, subject: &str) -> Mount
pub fn can(self, action: &str, subject: &str) -> Mount
Requires the ability action on the type subject (checked by the
RouteAuthorizer on every request). A mount
has no path parameter, so no record is named.
Sourcepub fn guard(self, guard: Arc<dyn Guard>) -> Mount
pub fn guard(self, guard: Arc<dyn Guard>) -> Mount
Adds a guard, run on every request before authorization and the service.
Sourcepub fn throttle(self, limit: u32, per: Duration) -> Mount
pub fn throttle(self, limit: u32, per: Duration) -> Mount
At most limit requests per per, per user (per client IP for
guests), counted for the whole mount: every path and method under the
prefix takes from one budget.
Sourcepub fn timeout(self, d: Duration) -> Mount
pub fn timeout(self, d: Duration) -> Mount
Overrides the app-wide request timeout (shorter or longer).
Sourcepub fn body_limit(self, bytes: usize) -> Mount
pub fn body_limit(self, bytes: usize) -> Mount
Overrides the app-wide request body limit: a request that declares a longer body is refused before the service is called, and a body sent without a length ends in an error once it passes the limit.
Sourcepub fn methods(self, methods: &[Method]) -> Mount
pub fn methods(self, methods: &[Method]) -> Mount
Only these methods (default: every method, extension methods such as
PROPFIND included; an empty list fails the boot). A method named
twice is kept once. With a list, HEAD runs the GET answer without
its body and OPTIONS lists the methods, as for a route.