1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! Various middleware implementations for JSON-RPC specific purposes.
pub mod layer;
pub use layer::*;
use futures_util::Future;
use jsonrpsee_core::server::MethodResponse;
use jsonrpsee_types::Request;
use layer::either::Either;
use tower::layer::util::{Identity, Stack};
use tower::layer::LayerFn;
/// Similar to the [`tower::Service`] but specific for jsonrpsee and
/// doesn't requires `&mut self` for performance reasons.
pub trait RpcServiceT<'a> {
/// The future response value.
type Future: Future<Output = MethodResponse> + Send;
/// Process a single JSON-RPC call it may be a subscription or regular call.
/// In this interface they are treated in the same way but it's possible to
/// distinguish those based on the `MethodResponse`.
fn call(&self, request: Request<'a>) -> Self::Future;
}
/// Similar to [`tower::ServiceBuilder`] but doesn't
/// support any tower middleware implementations.
#[derive(Debug, Clone)]
pub struct RpcServiceBuilder<L>(tower::ServiceBuilder<L>);
impl Default for RpcServiceBuilder<Identity> {
fn default() -> Self {
RpcServiceBuilder(tower::ServiceBuilder::new())
}
}
impl RpcServiceBuilder<Identity> {
/// Create a new [`RpcServiceBuilder`].
pub fn new() -> Self {
Self(tower::ServiceBuilder::new())
}
}
impl<L> RpcServiceBuilder<L> {
/// Optionally add a new layer `T` to the [`RpcServiceBuilder`].
///
/// See the documentation for [`tower::ServiceBuilder::option_layer`] for more details.
pub fn option_layer<T>(self, layer: Option<T>) -> RpcServiceBuilder<Stack<Either<T, Identity>, L>> {
let layer = if let Some(layer) = layer { Either::Left(layer) } else { Either::Right(Identity::new()) };
self.layer(layer)
}
/// Add a new layer `T` to the [`RpcServiceBuilder`].
///
/// See the documentation for [`tower::ServiceBuilder::layer`] for more details.
pub fn layer<T>(self, layer: T) -> RpcServiceBuilder<Stack<T, L>> {
RpcServiceBuilder(self.0.layer(layer))
}
/// Add a [`tower::Layer`] built from a function that accepts a service and returns another service.
///
/// See the documentation for [`tower::ServiceBuilder::layer_fn`] for more details.
pub fn layer_fn<F>(self, f: F) -> RpcServiceBuilder<Stack<LayerFn<F>, L>> {
RpcServiceBuilder(self.0.layer_fn(f))
}
/// Add a logging layer to [`RpcServiceBuilder`]
///
/// This logs each request and response for every call.
///
pub fn rpc_logger(self, max_log_len: u32) -> RpcServiceBuilder<Stack<RpcLoggerLayer, L>> {
RpcServiceBuilder(self.0.layer(RpcLoggerLayer::new(max_log_len)))
}
/// Wrap the service `S` with the middleware.
pub(crate) fn service<S>(&self, service: S) -> L::Service
where
L: tower::Layer<S>,
{
self.0.service(service)
}
}