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
use crate::store::LazyReqStore;
use core::{
convert::Infallible,
task::{Context, Poll},
};
use futures::future::{Future, TryFuture};
use http::Request;
use hyper::{service::Service, Body};
use warp::{
reply::{Reply, Response},
Filter, Rejection,
};
#[derive(Clone, Copy)]
pub struct JsonRpcService<S> {
service: S,
}
impl<S> Service<Request<Body>> for JsonRpcService<S>
where
S: Service<Request<Body>>,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(ctx)
}
fn call(&mut self, mut req: Request<Body>) -> Self::Future {
let ext = req.extensions_mut();
if ext.get::<LazyReqStore>().is_none() {
ext.insert(LazyReqStore::empty());
}
self.service.call(req)
}
}
impl<S> JsonRpcService<S> {
pub fn new(service: S) -> JsonRpcService<S> {
JsonRpcService { service }
}
}
pub fn service<F>(
filter: F,
) -> impl Service<
Request<Body>,
Response = Response,
Error = Infallible,
Future = impl Future<Output = Result<Response, Infallible>>,
> + Clone
where
F: Filter + Clone,
F::Future: TryFuture<Error = Rejection>,
<F::Future as TryFuture>::Ok: Reply,
{
JsonRpcService::new(warp::service(filter))
}