中文
Sentinel-Rust Middleware Supports
Currently Sentinel-Rust supports following RPC/Web frameworks, and provides thorough examples.
Tonic
Tonic is A rust implementation of gRPC, a high performance, open source, general RPC framework that puts mobile and HTTP/2 first.
The are two kinds of middlewares in Tonic.
- tonic::service::interceptor::Interceptor
We have implemented both of them, see sentinel-tower and sentinel-tonic on crates.io.
Here is a post related to its implementation.
Volo
Volo is a high-performance and strong-extensibility Rust RPC framework that helps developers build microservices.
Different from the Tower in Tonic, Volo uses the Motore for service abstraction.
For more information, see sentinel-motore on crates.io.
Here is a post related to its implementation.
Actix Web
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust
In general, a middleware in Actix Web is a type that implements the Service trait and Transform trait.
For more information, see sentinel-actix on crates.io.
Here is a post related to routers and handlers in the Actix-Web.
Rocket
Rocket is a web framework for Rust that makes it simple to write fast, secure web applications without sacrificing flexibility, usability, or type safety.
There are two ways to implement a Sentinel middleware in Rocket.
Intuitively, we can implement the Fairing
trait, just as the common Service
traits in other frameworks.
However, as documented in the Rocket guide,
Rocket’s fairings are a lot like middleware from other frameworks, but they bear a few key distinctions:
- Fairings cannot terminate or respond to an incoming request directly.
- Fairings cannot inject arbitrary, non-request data into a request.
- Fairings can prevent an application from launching.
- Fairings can inspect and modify the application's configuration.
Since it cannot terminate or respond to the request directly, the implemented SentinelFairing
simply rewrites the URI in the Request to a given route. It can be configured via its own methods or managed state of SentinelConfig
.
In fact, Rocket suggests using request guards, instead of Fairing in this case,
As a general rule of thumb, only globally applicable actions should be effected through fairings. You should *not* use a fairing to implement authentication or authorization (preferring to use a request guard instead) unless the authentication or authorization applies to all or the overwhelming majority of the application. On the other hand, you should use a fairing to record timing and usage statistics or to enforce global security policies.
So we follow this suggestion
Request guards appear as inputs to handlers. An arbitrary number of request guards can appear as arguments in a route handler. Rocket will automatically invoke the
FromRequest
implementation for request guards before calling the handler. Rocket only dispatches requests to a handler when all of its guards pass.
and implemented a SentinelGuard
. It can be configured via the managed state of SentinelConfig
,
For more information, see sentinel-rocket on crates.io.
Axum
Axum is a web application framework that focuses on ergonomics and modularity.
In particular the last point is what sets
axum
apart from other frameworks.axum
doesn't have its own middleware system but instead usestower::Service
. This meansaxum
gets timeouts, tracing, compression, authorization, and more, for free. It also enables you to share middleware with applications written usinghyper
ortonic
.
Therefore, we can reuse the middleware in sentinel-tower. For more information, visit our example for Axum.
One More Thing
Currently, dynamic datasources for sentinel are implemented directly in sentinel-core as customized features. Maybe similar to these middlewares, splitting datasources into individual crates or a single crate with customized features is better...