#json-rpc #rpc #warp

warp-json-rpc

JSON RPC server extension for warp

10 unstable releases (3 breaking)

0.3.0 Jan 28, 2021
0.2.0 Oct 23, 2020
0.1.6 Mar 13, 2020
0.0.1 Feb 18, 2020

#15 in #warp

Download history 106/week @ 2022-11-27 69/week @ 2022-12-04 58/week @ 2022-12-11 63/week @ 2022-12-18 58/week @ 2022-12-25 53/week @ 2023-01-01 50/week @ 2023-01-08 61/week @ 2023-01-15 94/week @ 2023-01-22 93/week @ 2023-01-29 160/week @ 2023-02-05 88/week @ 2023-02-12 163/week @ 2023-02-19 68/week @ 2023-02-26 105/week @ 2023-03-05 86/week @ 2023-03-12

445 downloads per month
Used in 2 crates

MIT/Apache

20KB
471 lines

Bring JSON RPC features into warp world.

Filters

  • json_rpc
    • requires the request to follow basic JSON RPC specification.
    • YOU HAVE TO CALL THIS FILTER BEFORE ANY OF BELOW FILTERS.
  • method
    • requires the request RPC method to be given name.
  • params
    • extracts RPC parameter.

Example

use warp_json_rpc::filters as json_rpc;
use futures::future;
use warp::Filter as _;

#[tokio::main]
async fn main() {
  // create Filter
  let route = warp::filters::path::path("rpc")
    // ## Point 1
    // This filter is required.
    .and(json_rpc::json_rpc())
    .and(json_rpc::method("add"))
    .and(json_rpc::params::<(usize, usize)>())
    // `res.success` returns `impl Reply` which represents JSON RPC Response
    .map(|res: Builder, (lhs, rhs)| res.success(lhs + rhs).unwrap());

  // ## Point 2
  // You **MUST** wraps root `Filter` by `warp_json_rpc::service` function.
  let svc = warp_json_rpc::service(route);
  let make_svc = hyper::service::make_service_fn(move |_| future::ok::<_, Infallible>(svc));

  hyper::Server::bind(&([127, 0, 0, 1], 3030).into())
    .serve(make_svc)
    .await
    .unwrap();
}

Dependencies

~9–16MB
~288K SLoC