1 unstable release

0.5.0-alpha Aug 4, 2020

#588 in #web-framework

MIT license

170KB
3K SLoC

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(())
}

Dependencies

~13MB
~251K SLoC