jsonrpc-v2 0.3.2

JSONRPC 2.0 server
Documentation

jsonrpc-v2

Docs Crates.io

A very small and very fast JSON-RPC 2.0 server-focused framework.

Provides integrations for both hyper and actix-web. Enable features actix or hyper depending on need.

actix is enabled by default. Make sure to add default-features = false if using hyper.

Also see the easy-errors feature flag (not enabled by default). Enabling this flag will implement ErrorLike for anything that implements Display, and the display value will be provided in the message field of the JSON-RPC 2.0 Error response.

Otherwise, custom errors should implement ErrorLike to map errors to the JSON-RPC 2.0 Error response.

Individual method handlers can take various kinds of args (things that can be extracted from the request, like the Params or State), and should return something that can resolve into a future where the Item is serializable. See examples below.

Usage

use jsonrpc_v2::*;

#[derive(serde::Deserialize)]
struct TwoNums { a: usize, b: usize }

fn add(Params(params): Params<TwoNums>) -> Result<usize, Error> {
    Ok(params.a + params.b)
}

fn sub(Params(params): Params<(usize, usize)>) -> Result<usize, Error> {
    Ok(params.0 - params.1)
}

fn message(state: State<String>) -> Result<String, Error> {
    Ok(String::from(&*state))
}

fn main() -> Result<(), Box<dyn std::error::Error>> {

    let rpc = Server::with_state(String::from("Hello!"))
        .with_method("add", add)
        .with_method("sub", sub)
        .with_method("message", message)
        .finish();

    actix_web::HttpServer::new(move || {
        let rpc = rpc.clone();
        actix_web::App::new().service(
            actix_web::web::service("/api")
                .guard(actix_web::guard::Post())
                .finish(rpc.into_web_service()),
        )
    })
    .bind("0.0.0.0:3000")?
    .run()?;

    Ok(())
}

Current version: 0.3.2

License: MIT