Skip to content
/ go-rpc Public

A simple framework for creating JSON rpc servers.

License

Notifications You must be signed in to change notification settings

bubunyo/go-rpc

Repository files navigation

go-rpc

Build Status GoDoc

A simple resource for creating JSON RPC servers to comply with the JSON RPC Spec

Features

  1. Bootstrap your json RPC Server with ease.
  2. Handle multiple request concurrently.
  3. Add your own custom error

Example Usage

  1. Setup a Ping service
package main

type PingService struct{}

func (s PingService) Echo(_ context.Context, req *rpc.RequestParams) (any, error) {
	return "ok", nil
}

func (s PingService) Register() (string, rpc.RequestMap) {
	return "PingService", map[string]rpc.RequestFunc{
		"Ping": s.Echo,
	}
}

func main() {
  server := rpc.NewDefaultServer()
	server.AddService(PingService{})

	mux := http.NewServeMux()
	mux.Handle("/rpc", server)
	log.Fatalln(http.ListenAndServe(":8080", mux))

You can consume a single ping service resource with this curl request

curl -X POST  localhost:8080/rpc \
-d '{
        "jsonrpc": "2.0",
        "id": null,
        "method": "PingService.Ping",
        "params": null
}'

with the following output

{
	"jsonrpc": "2.0",
	"id": null,
	"result": "ok"
}

or multiple resources using

~ curl -X POST  localhost:8080/rpc -d '[{
        "jsonrpc": "2.0",
        "id": null,
        "method": "PingService.Ping",
        "params": null
}, {
        "jsonrpc": "2.0",
        "id": null,
        "method": "PingService.Ping",
        "params": null
}, {
        "jsonrpc": "2.0",
        "id": null,
        "method": "PingService.Ping",
        "params": null
}]'

with the following output

[{
	"jsonrpc": "2.0",
	"id": null,
	"result": "ok"
}, {
	"jsonrpc": "2.0",
	"id": null,
	"result": "ok"
}, {
	"jsonrpc": "2.0",
	"id": null,
	"result": "ok"
}]

About

A simple framework for creating JSON rpc servers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages