A simple resource for creating JSON RPC servers to comply with the JSON RPC Spec
- Bootstrap your json RPC Server with ease.
- Handle multiple request concurrently.
- Add your own custom error
- 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"
}]