Skip to content
/ jsonrpc Public

A simple Go JSONRPC 2.0 client/server over HTTP.

License

Notifications You must be signed in to change notification settings

echovl/jsonrpc

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"
)

type User struct {
        ID   int    `json:"id"`
        Name string `json:"name"`
}

func getUser(ctx context.Context, id string) (User, error) {
        return User{ID: 1, Name: "Jhon Doe"}, nil
}

func main() {
        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"
)

type User struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func main() {
	client := jsonrpc.NewClient("http://127.0.0.1:4545/api")
	user := &User{}
	resp, err := client.Call(context.Background(), "getUserById", "id")
	if err != nil {
		panic(err)
	}
    	if err := resp.Decode(user); err != nil {
		panic(err)
    	}
	fmt.Println("user: ", user)
}

About

A simple Go JSONRPC 2.0 client/server over HTTP.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages