roa-jsonrpc

Crates.ioroa-jsonrpc
lib.rsroa-jsonrpc
version0.5.0-alpha
sourcesrc
created_at2020-08-04 07:15:44.865995
updated_at2020-08-04 07:15:44.865995
descriptionjsonrpc integration for roa
homepagehttps://github.com/Hexilee/roa/wiki
repositoryhttps://github.com/Hexilee/roa
max_upload_size
id272805
size4,784
xixi (Hexilee)

documentation

https://docs.rs/roa-jsonrpc

README

Stable Test codecov Rust Docs Crate version Download MSRV-1.40 License: MIT

roa-jsonrpc

This crate provides a json rpc endpoint.

Example

use roa::App;
use roa_jsonrpc::{RpcEndpoint, Data, Error, Params, Server};
use std::error::Error as StdError;

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

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

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

async fn message(data: Data<String>) -> Result<String, Error> {
    Ok(String::from(&*data))
}

#[async_std::main]
async fn main() -> std::io::Result<()> {
    let rpc = Server::new()
        .with_data(Data::new(String::from("Hello!")))
        .with_method("sub", sub)
        .with_method("message", message)
        .finish();

    let app = App::new().end(RpcEndpoint(rpc));
    app.listen("127.0.0.1:8000", |addr| {
        info!("Server is listening on {}", addr)
    })?
    .await?;
    Ok(())
}
Commit count: 722

cargo fmt