warp-json-rpc

Crates.iowarp-json-rpc
lib.rswarp-json-rpc
version0.3.0
sourcesrc
created_at2020-02-18 06:41:37.322085
updated_at2021-01-28 13:23:39.4674
descriptionJSON RPC server extension for warp
homepage
repositoryhttps://github.com/AtsukiTak/warp-json-rpc
max_upload_size
id210237
size56,430
Atsuki Takahashi (AtsukiTak)

documentation

README

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();
}
Commit count: 86

cargo fmt