You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. # tendermint/rpc/lib
  2. [![CircleCI](https://circleci.com/gh/tendermint/tendermint/rpc/lib.svg?style=svg)](https://circleci.com/gh/tendermint/tendermint/rpc/lib)
  3. HTTP RPC server supporting calls via uri params, jsonrpc, and jsonrpc over websockets
  4. # Client Requests
  5. Suppose we want to expose the rpc function `HelloWorld(name string, num int)`.
  6. ## GET (URI)
  7. As a GET request, it would have URI encoded parameters, and look like:
  8. ```
  9. curl 'http://localhost:8008/hello_world?name="my_world"&num=5'
  10. ```
  11. Note the `'` around the url, which is just so bash doesn't ignore the quotes in `"my_world"`.
  12. This should also work:
  13. ```
  14. curl http://localhost:8008/hello_world?name=\"my_world\"&num=5
  15. ```
  16. A GET request to `/` returns a list of available endpoints.
  17. For those which take arguments, the arguments will be listed in order, with `_` where the actual value should be.
  18. ## POST (JSONRPC)
  19. As a POST request, we use JSONRPC. For instance, the same request would have this as the body:
  20. ```
  21. {
  22. "jsonrpc": "2.0",
  23. "id": "anything",
  24. "method": "hello_world",
  25. "params": {
  26. "name": "my_world",
  27. "num": 5
  28. }
  29. }
  30. ```
  31. With the above saved in file `data.json`, we can make the request with
  32. ```
  33. curl --data @data.json http://localhost:8008
  34. ```
  35. ## WebSocket (JSONRPC)
  36. All requests are exposed over websocket in the same form as the POST JSONRPC.
  37. Websocket connections are available at their own endpoint, typically `/websocket`,
  38. though this is configurable when starting the server.
  39. # Server Definition
  40. Define some types and routes:
  41. ```
  42. type ResultStatus struct {
  43. Value string
  44. }
  45. // Define some routes
  46. var Routes = map[string]*rpcserver.RPCFunc{
  47. "status": rpcserver.NewRPCFunc(Status, "arg"),
  48. }
  49. // an rpc function
  50. func Status(v string) (*ResultStatus, error) {
  51. return &ResultStatus{v}, nil
  52. }
  53. ```
  54. Now start the server:
  55. ```
  56. mux := http.NewServeMux()
  57. rpcserver.RegisterRPCFuncs(mux, Routes)
  58. wm := rpcserver.NewWebsocketManager(Routes, nil)
  59. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  60. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  61. go func() {
  62. _, err := rpcserver.StartHTTPServer("0.0.0.0:8008", mux, logger)
  63. if err != nil {
  64. panic(err)
  65. }
  66. }()
  67. ```
  68. Note that unix sockets are supported as well (eg. `/path/to/socket` instead of `0.0.0.0:8008`)
  69. Now see all available endpoints by sending a GET request to `0.0.0.0:8008`.
  70. Each route is available as a GET request, as a JSONRPCv2 POST request, and via JSONRPCv2 over websockets.
  71. # Examples
  72. * [Tendermint](https://github.com/tendermint/tendermint/blob/master/rpc/core/routes.go)
  73. * [tm-monitor](https://github.com/tendermint/tools/blob/master/tm-monitor/rpc.go)
  74. ## CHANGELOG
  75. ### 0.7.0
  76. BREAKING CHANGES:
  77. - removed `Client` empty interface
  78. - `ClientJSONRPC#Call` `params` argument became a map
  79. - rename `ClientURI` -> `URIClient`, `ClientJSONRPC` -> `JSONRPCClient`
  80. IMPROVEMENTS:
  81. - added `HTTPClient` interface, which can be used for both `ClientURI`
  82. and `ClientJSONRPC`
  83. - all params are now optional (Golang's default will be used if some param is missing)
  84. - added `Call` method to `WSClient` (see method's doc for details)