Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware<S>: Send
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,
{ // 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§

Source

fn name(&self) -> String

Source

fn enabled(&self, state: &S) -> bool

Source

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.

Source

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

Implementors§

Source§

impl<S> Middleware<S> for AnyMiddleware<S>
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for CacheControlMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for CatchPanicMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for CorsMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for EtagMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for PropagateRequestIdMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for RequestBodyLimitMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for RequestDecompressionMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for RequestResponseLoggingMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for ResponseCompressionMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for SensitiveRequestHeadersMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for SensitiveResponseHeadersMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for SetRequestIdMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for TimeoutMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,

Source§

impl<S> Middleware<S> for TracingMiddleware
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>,