Expand description
A very small and very fast JSON-RPC 2.0 server-focused framework.
Provides integrations for both hyper
and actix-web
(1.x, 2.x, 3.x, 4.x).
Enable features actix-web-v3-integration
, hyper-integration
, etc. as needed.
actix-web-v4-integration
is enabled by default. Make sure to add default-features = false
if using hyper
or other actix-web
versions.
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 are async
functions that can take various kinds of args (things that can be extracted from the request, like
the Params
or Data
), and should return a Result<Item, Error>
where the Item
is serializable. See examples below.
§Usage
use jsonrpc_v2::{Data, Error, Params, Server};
#[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))
}
#[actix_rt::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();
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()
.await
}
Structs§
- Boxed
Handler - Data
- Data/state storage container
- MapRouter
- Method
- Method string wrapper, for
FromRequest
extraction - Notification
Builder - Builder struct for a notification request object
- Params
FromRequest
wrapper for request params- Request
Builder - Builder struct for a request object
- Request
Object - Request/Notification object
- Server
- Server/request handler
- Server
Builder - Builder used to add methods to a server
Enums§
- Error
- Error object in a response
- Id
- Container for the request ID, which can be a string, number, or null. Not typically used directly.
- Request
Kind - An enum to contain the different kinds of possible requests: using the provided
RequestObject
, an array ofRequestObject
s, or raw bytes. - Response
Object - The individual response object
- Response
Objects - Container for the response object(s) or
Empty
for notification request(s)
Traits§
- Error
Like - Trait that can be used to map custom errors to the
Error
object. - From
Request - A trait to extract data from the request
- Router