-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,57 @@ | ||
# jsonrpc2/v7 | ||
# jsonrpc2/v9 | ||
[![GoDoc](https://godoc.org/github.com/AdamSLevy/jsonrpc2?status.svg)](https://godoc.org/github.com/AdamSLevy/jsonrpc2) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/AdamSLevy/jsonrpc2)](https://goreportcard.com/report/github.com/AdamSLevy/jsonrpc2) | ||
[![Coverage Status](https://coveralls.io/repos/github/AdamSLevy/jsonrpc2/badge.svg?branch=master)](https://coveralls.io/github/AdamSLevy/jsonrpc2?branch=master) | ||
[![Build Status](https://travis-ci.org/AdamSLevy/jsonrpc2.svg?branch=master)](https://travis-ci.org/AdamSLevy/jsonrpc2) | ||
|
||
Package `jsonrpc2` is a conforming implementation of the JSON-RPC 2.0 protocol | ||
designed to provide a minimalist API, avoid unnecessary unmarshaling and memory | ||
allocation, and work with any http server framework that uses http.Handler. | ||
|
||
The `HTTPRequestHandler` will recover from any `MethodFunc` panics and will | ||
always respond with a valid JSON RPC Response, unless of course the request was | ||
a notification. | ||
Package `jsonrpc2` is a complete and strictly conforming implementation of the | ||
JSON-RPC 2.0 protocol for both clients and servers. | ||
|
||
It strives to conform to the official specification: | ||
[https://www.jsonrpc.org](https://www.jsonrpc.org) | ||
|
||
|
||
## Getting started | ||
Please read the official godoc documentation for the most up to date | ||
information. | ||
documentation. | ||
|
||
### Client | ||
|
||
Clients use the provided types, optionally along with their own custom data | ||
types for making `Requests` and parsing `Response`s. The `Request` and | ||
`Response` types are defined so that they can accept any valid types for | ||
`"id"`, `"params"`, and `"result"`. | ||
|
||
Clients can use the `Request`, `Response`, and `Error` types with the `json` | ||
and `http` packages to make HTTP JSON-RPC 2.0 calls and parse their responses. | ||
```go | ||
reqBytes, _ := json.Marshal(jsonrpc2.NewRequest("subtract", 0, []int{5, 1})) | ||
httpResp, _ := http.Post("www.example.com", "application/json", | ||
bytes.NewReader(reqBytes)) | ||
respBytes, _ := ioutil.ReadAll(httpResp.Body) | ||
response := jsonrpc2.Response{} | ||
response := jsonrpc2.Response{Result: &MyCustomResultType{}} | ||
json.Unmarshal(respBytes, &response) | ||
``` | ||
|
||
### Server | ||
|
||
Servers must implement their RPC method functions to match the `MethodFunc` | ||
type, and relate a name to the method using a `MethodMap`. | ||
```go | ||
var func versionMethod(p json.RawMessage) jsonrpc2.Response { | ||
if p != nil { | ||
return jsonrpc2.NewInvalidParamsErrorResponse(nil) | ||
} | ||
return jrpc.NewResponse("0.0.0") | ||
} | ||
var methods = MethodMap{"version", versionMethod} | ||
``` | ||
Read the documentation for MethodFunc and MethodMap for more information. | ||
Servers define their own `MethodFunc`s and associate them with a method name in | ||
a `MethodMap`. Passing the `MethodMap` to `HTTPRequestHandler()` will return a | ||
corresponding `http.Handler` which can be used with an `http.Server`. The | ||
`http.Handler` handles both batch and single requests, catches all protocol | ||
errors, and recovers from any panics or invalid return values from the user | ||
provided `MethodFunc`. `MethodFunc`s only need to catch errors related to their | ||
function such as `InvalidParams` or any user defined errors for the RPC method. | ||
|
||
Finally generate an http.HandlerFunc for your MethodMap and start your server. | ||
```go | ||
http.ListenAndServe(":8080", jsonrpc2.HTTPRequestHandler(methods)) | ||
func versionMethod(params json.RawMessage) interface{} { | ||
if params != nil { | ||
return jsonrpc2.NewInvalidParamsError("no params accepted") | ||
} | ||
return "0.0.0" | ||
} | ||
var methods = jsonrpc2.MethodMap{"version": versionMethod} | ||
func StartServer() { | ||
http.ListenAndServe(":8080", jsonrpc2.HTTPRequestHandler(methods)) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters