2017-04-26 19:57:33 -04:00
|
|
|
# tendermint/rpc/lib
|
2016-01-12 16:50:06 -05:00
|
|
|
|
2017-04-26 19:57:33 -04:00
|
|
|
[](https://circleci.com/gh/tendermint/tendermint/rpc/lib)
|
2016-06-21 15:19:56 -04:00
|
|
|
|
2016-01-12 16:50:06 -05:00
|
|
|
HTTP RPC server supporting calls via uri params, jsonrpc, and jsonrpc over websockets
|
|
|
|
|
2016-06-23 20:33:04 -04:00
|
|
|
# Client Requests
|
|
|
|
|
|
|
|
Suppose we want to expose the rpc function `HelloWorld(name string, num int)`.
|
|
|
|
|
|
|
|
## GET (URI)
|
|
|
|
|
|
|
|
As a GET request, it would have URI encoded parameters, and look like:
|
|
|
|
|
|
|
|
```
|
|
|
|
curl 'http://localhost:8008/hello_world?name="my_world"&num=5'
|
|
|
|
```
|
|
|
|
|
|
|
|
Note the `'` around the url, which is just so bash doesn't ignore the quotes in `"my_world"`.
|
|
|
|
This should also work:
|
|
|
|
|
|
|
|
```
|
|
|
|
curl http://localhost:8008/hello_world?name=\"my_world\"&num=5
|
|
|
|
```
|
|
|
|
|
|
|
|
A GET request to `/` returns a list of available endpoints.
|
|
|
|
For those which take arguments, the arguments will be listed in order, with `_` where the actual value should be.
|
|
|
|
|
|
|
|
## POST (JSONRPC)
|
|
|
|
|
|
|
|
As a POST request, we use JSONRPC. For instance, the same request would have this as the body:
|
|
|
|
|
|
|
|
```
|
|
|
|
{
|
2017-03-07 18:34:54 +04:00
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": "anything",
|
|
|
|
"method": "hello_world",
|
|
|
|
"params": {
|
|
|
|
"name": "my_world",
|
|
|
|
"num": 5
|
|
|
|
}
|
2016-06-23 20:33:04 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
With the above saved in file `data.json`, we can make the request with
|
|
|
|
|
|
|
|
```
|
|
|
|
curl --data @data.json http://localhost:8008
|
|
|
|
```
|
|
|
|
|
|
|
|
## WebSocket (JSONRPC)
|
|
|
|
|
2017-03-07 18:34:54 +04:00
|
|
|
All requests are exposed over websocket in the same form as the POST JSONRPC.
|
|
|
|
Websocket connections are available at their own endpoint, typically `/websocket`,
|
2016-06-23 20:33:04 -04:00
|
|
|
though this is configurable when starting the server.
|
|
|
|
|
|
|
|
# Server Definition
|
2016-06-21 15:19:56 -04:00
|
|
|
|
|
|
|
Define some types and routes:
|
|
|
|
|
|
|
|
```
|
|
|
|
type ResultStatus struct {
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define some routes
|
|
|
|
var Routes = map[string]*rpcserver.RPCFunc{
|
2017-04-28 23:18:38 -04:00
|
|
|
"status": rpcserver.NewRPCFunc(Status, "arg"),
|
2016-06-21 15:19:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// an rpc function
|
2017-04-28 23:18:38 -04:00
|
|
|
func Status(v string) (*ResultStatus, error) {
|
2016-06-21 15:19:56 -04:00
|
|
|
return &ResultStatus{v}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
Now start the server:
|
|
|
|
|
|
|
|
```
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
rpcserver.RegisterRPCFuncs(mux, Routes)
|
|
|
|
wm := rpcserver.NewWebsocketManager(Routes, nil)
|
|
|
|
mux.HandleFunc("/websocket", wm.WebsocketHandler)
|
2017-05-02 11:53:32 +04:00
|
|
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
2016-06-21 15:19:56 -04:00
|
|
|
go func() {
|
2017-05-02 11:53:32 +04:00
|
|
|
_, err := rpcserver.StartHTTPServer("0.0.0.0:8008", mux, logger)
|
2016-06-21 15:19:56 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2016-06-23 20:33:04 -04:00
|
|
|
Note that unix sockets are supported as well (eg. `/path/to/socket` instead of `0.0.0.0:8008`)
|
2016-06-21 15:19:56 -04:00
|
|
|
|
2016-06-23 20:33:04 -04:00
|
|
|
Now see all available endpoints by sending a GET request to `0.0.0.0:8008`.
|
2017-03-07 18:34:54 +04:00
|
|
|
Each route is available as a GET request, as a JSONRPCv2 POST request, and via JSONRPCv2 over websockets.
|
2016-06-21 15:19:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
|
|
|
* [Tendermint](https://github.com/tendermint/tendermint/blob/master/rpc/core/routes.go)
|
2017-03-13 14:25:57 +04:00
|
|
|
* [tm-monitor](https://github.com/tendermint/tools/blob/master/tm-monitor/rpc.go)
|
2017-03-09 12:43:24 +04:00
|
|
|
|
|
|
|
## CHANGELOG
|
|
|
|
|
|
|
|
### 0.7.0
|
|
|
|
|
|
|
|
BREAKING CHANGES:
|
|
|
|
|
|
|
|
- removed `Client` empty interface
|
|
|
|
- `ClientJSONRPC#Call` `params` argument became a map
|
2017-03-08 01:22:35 +04:00
|
|
|
- rename `ClientURI` -> `URIClient`, `ClientJSONRPC` -> `JSONRPCClient`
|
2017-03-09 12:43:24 +04:00
|
|
|
|
|
|
|
IMPROVEMENTS:
|
|
|
|
|
|
|
|
- added `HTTPClient` interface, which can be used for both `ClientURI`
|
|
|
|
and `ClientJSONRPC`
|
|
|
|
- all params are now optional (Golang's default will be used if some param is missing)
|
2017-03-10 15:23:43 +04:00
|
|
|
- added `Call` method to `WSClient` (see method's doc for details)
|