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