Skip to content

Repository files navigation

JSON-RPC 2.0 Module for golang

A go implementation of JSON RPC 2.0 over http. Work in progress.

Installing

To start using this library, install Go 1.13 or above. Run the following command to retrieve the library.

$ go get -u github.com/echovl/jsonrpc

Server

package main
import (
"context""net/http""github.com/echovl/jsonrpc"
)
typeUserstruct {
IDint`json:"id"`Namestring`json:"name"`
}
funcgetUser(ctx context.Context, idstring) (User, error) {
returnUser{ID: 1, Name: "Jhon Doe"}, nil
}
funcmain() {
server:=jsonrpc.NewServer()
server.HandleFunc("getUserById", getUser)
http.Handle("/api", server)
http.ListenAndServe(":4545", nil)
}

Client

package main
import (
"context""fmt""github.com/echovl/jsonrpc"
)
typeUserstruct {
IDint`json:"id"`Namestring`json:"name"`
}
funcmain() {
client:=jsonrpc.NewClient("http://127.0.0.1:4545/api")
user:=&User{}
resp, err:=client.Call(context.Background(), "getUserById", "id")
iferr!=nil {
panic(err)
}
iferr:=resp.Decode(user); err!=nil {
panic(err)
}
fmt.Println("user: ", user)
}

About

A simple Go JSONRPC 2.0 client/server over HTTP.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages