github.com/vipernet-xyz/tm@v0.34.24/rpc/jsonrpc/doc.go (about)

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