Crates.io | roa-jsonrpc |
lib.rs | roa-jsonrpc |
version | 0.5.0-alpha |
source | src |
created_at | 2020-08-04 07:15:44.865995 |
updated_at | 2020-08-04 07:15:44.865995 |
description | jsonrpc integration for roa |
homepage | https://github.com/Hexilee/roa/wiki |
repository | https://github.com/Hexilee/roa |
max_upload_size | |
id | 272805 |
size | 4,784 |
This crate provides a json rpc endpoint.
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(())
}