jsonrpc

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

HTTP RPC server supporting calls via uri params, jsonrpc over HTTP, and jsonrpc over websockets

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:

{
  "jsonrpc": "2.0",
  "id": "anything",
  "method": "hello_world",
  "params": {
    "name": "my_world",
    "num": 5
  }
}

With the above saved in file `data.json`, we can make the request with

curl --data @data.json http://localhost:8008

WebSocket (JSONRPC)

All requests are exposed over websocket in the same form as the POST JSONRPC. Websocket connections are available at their own endpoint, typically `/websocket`, though this is configurable when starting the server.

Server Definition

Define some types and routes:

type ResultStatus struct {
	    Value string
}

Define some routes

  var Routes = map[string]*rpcserver.RPCFunc{
	    "status": rpcserver.NewRPCFunc(Status, "arg"),
  }

An rpc function:

  func Status(v string) (*ResultStatus, error) {
	    return &ResultStatus{v}, nil
  }

Now start the server:

mux := http.NewServeMux()
rpcserver.RegisterRPCFuncs(mux, Routes)
wm := rpcserver.NewWebsocketManager(Routes)
mux.HandleFunc("/websocket", wm.WebsocketHandler)
logger := log.NewLogger(os.Stdout)
listener, err := rpc.Listen("0.0.0.0:8080", rpcserver.Config{})
if err != nil { panic(err) }
go rpcserver.Serve(listener, mux, logger)

Note that unix sockets are supported as well (eg. `/path/to/socket` instead of `0.0.0.0:8008`) Now see all available endpoints by sending a GET request to `0.0.0.0:8008`. Each route is available as a GET request, as a JSONRPCv2 POST request, and via JSONRPCv2 over websockets.

Examples

- [PellDVS](https://github.com/0xPellNetwork/pelldvs/blob/v0.38.x/rpc/core/routes.go)

Index

Constants

This section is empty.

Variables

View Source
var Routes = map[string]*server.RPCFunc{
	"echo":            server.NewRPCFunc(EchoResult, "arg"),
	"echo_ws":         server.NewWSRPCFunc(EchoWSResult, "arg"),
	"echo_bytes":      server.NewRPCFunc(EchoBytesResult, "arg"),
	"echo_data_bytes": server.NewRPCFunc(EchoDataBytesResult, "arg"),
	"echo_int":        server.NewRPCFunc(EchoIntResult, "arg"),
	"echo_default":    server.NewRPCFunc(EchoWithDefault, "arg", server.Cacheable("arg")),
}

Define some routes

Functions

func TestHexStringArg

func TestHexStringArg(t *testing.T)

func TestJSONRPCCaching

func TestJSONRPCCaching(t *testing.T)

func TestMain

func TestMain(m *testing.M)

func TestQuotedStringArg

func TestQuotedStringArg(t *testing.T)

func TestServersAndClientsBasic

func TestServersAndClientsBasic(t *testing.T)

func TestURICaching

func TestURICaching(t *testing.T)

func TestWSClientPingPong

func TestWSClientPingPong(t *testing.T)

TestWSClientPingPong checks that a client & server exchange pings & pongs so connection stays alive.

func TestWSHandlesArrayParams

func TestWSHandlesArrayParams(t *testing.T)

func TestWSNewWSRPCFunc

func TestWSNewWSRPCFunc(t *testing.T)

Types

type ResultEcho

type ResultEcho struct {
	Value string `json:"value"`
}

func EchoResult

func EchoResult(_ *types.Context, v string) (*ResultEcho, error)

func EchoWSResult

func EchoWSResult(_ *types.Context, v string) (*ResultEcho, error)

type ResultEchoBytes

type ResultEchoBytes struct {
	Value []byte `json:"value"`
}

func EchoBytesResult

func EchoBytesResult(_ *types.Context, v []byte) (*ResultEchoBytes, error)

type ResultEchoDataBytes

type ResultEchoDataBytes struct {
	Value cmtbytes.HexBytes `json:"value"`
}

func EchoDataBytesResult

func EchoDataBytesResult(_ *types.Context, v cmtbytes.HexBytes) (*ResultEchoDataBytes, error)

type ResultEchoInt

type ResultEchoInt struct {
	Value int `json:"value"`
}

func EchoIntResult

func EchoIntResult(_ *types.Context, v int) (*ResultEchoInt, error)

type ResultEchoWithDefault

type ResultEchoWithDefault struct {
	Value int `json:"value"`
}

func EchoWithDefault

func EchoWithDefault(_ *types.Context, v *int) (*ResultEchoWithDefault, error)

Directories

Path Synopsis
Commons for HTTP handling
Commons for HTTP handling

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL