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.

111 lines
2.8 KiB

  1. # go-rpc
  2. [![CircleCI](https://circleci.com/gh/tendermint/go-rpc.svg?style=svg)](https://circleci.com/gh/tendermint/go-rpc)
  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":["my_world", 5]
  26. }
  27. ```
  28. Note the `params` does not currently support key-value pairs (https://github.com/tendermint/go-rpc/issues/1), so order matters (you can get the order from making a
  29. GET request to `/`)
  30. With the above saved in file `data.json`, we can make the request with
  31. ```
  32. curl --data @data.json http://localhost:8008
  33. ```
  34. ## WebSocket (JSONRPC)
  35. All requests are exposed over websocket in the same form as the POST JSONRPC.
  36. Websocket connections are available at their own endpoint, typically `/websocket`,
  37. though this is configurable when starting the server.
  38. # Server Definition
  39. Define some types and routes:
  40. ```
  41. // Define a type for results and register concrete versions with go-wire
  42. type Result interface{}
  43. type ResultStatus struct {
  44. Value string
  45. }
  46. var _ = wire.RegisterInterface(
  47. struct{ Result }{},
  48. wire.ConcreteType{&ResultStatus{}, 0x1},
  49. )
  50. // Define some routes
  51. var Routes = map[string]*rpcserver.RPCFunc{
  52. "status": rpcserver.NewRPCFunc(StatusResult, "arg"),
  53. }
  54. // an rpc function
  55. func StatusResult(v string) (Result, error) {
  56. return &ResultStatus{v}, nil
  57. }
  58. ```
  59. Now start the server:
  60. ```
  61. mux := http.NewServeMux()
  62. rpcserver.RegisterRPCFuncs(mux, Routes)
  63. wm := rpcserver.NewWebsocketManager(Routes, nil)
  64. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  65. go func() {
  66. _, err := rpcserver.StartHTTPServer("0.0.0.0:8008", mux)
  67. if err != nil {
  68. panic(err)
  69. }
  70. }()
  71. ```
  72. Note that unix sockets are supported as well (eg. `/path/to/socket` instead of `0.0.0.0:8008`)
  73. Now see all available endpoints by sending a GET request to `0.0.0.0:8008`.
  74. Each route is available as a GET request, as a JSONRPCv2 POST request, and via JSONRPCv2 over websockets
  75. # Examples
  76. * [Tendermint](https://github.com/tendermint/tendermint/blob/master/rpc/core/routes.go)
  77. * [Network Monitor](https://github.com/tendermint/netmon/blob/master/handlers/routes.go)