pub trait Middleware<S>: Send{
// Required methods
fn name(&self) -> String;
fn enabled(&self, state: &S) -> bool;
fn priority(&self, state: &S) -> i32;
fn install(&self, router: Router, state: &S) -> RoadsterResult<Router>;
}Expand description
Allows initializing and installing middleware on the app’s Router.
This trait is provided in addition to crate::service::http::initializer::Initializer because installing middleware is a bit of a special case compared to a general initializer: 1. The order in which middleware runs matters. For example, we want tower_http::sensitive_headers::SetSensitiveRequestHeadersLayer to run before tower_http::trace::TraceLayer to avoid logging sensitive headers. 2. Because of how axum’s Router::layer method installs middleware, the order in which middleware is installed is the reverse of the order it will run when handling a request. Therefore, we install the middleware in the reverse order that we want it to run (this is done automatically by Roadster based on Middleware::priority).
Required Methods§
fn name(&self) -> String
fn enabled(&self, state: &S) -> bool
Sourcefn priority(&self, state: &S) -> i32
fn priority(&self, state: &S) -> i32
Used to determine the order in which the middleware will run when handling a request. Smaller
numbers will run before larger numbers. For example, a middleware with priority -10
will run before a middleware with priority 10.
If two middlewares have the same priority, they are not guaranteed to run or be installed in any particular order relative to each other. This may be fine for many middlewares.
If the order in which your middleware runs doesn’t particularly matter, it’s generally
safe to set its priority as 0.
Note: Because of how axum’s Router::layer method installs middleware, the order in which
middleware is installed is the reverse of the order it will run when handling a request.
Therefore, we install the middleware in the reverse order that we want it to run (this
is done automatically by Roadster based on Middleware::priority). So, a middleware
with priority -10 will be installed after a middleware with priority 10, which will
allow the middleware with priority -10 to run before a middleware with priority 10.
fn install(&self, router: Router, state: &S) -> RoadsterResult<Router>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".