jsonrpc

package module
v0.0.0-...-33569dd Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2019 License: MIT Imports: 6 Imported by: 0

README

jsonrpc

Go Report Card GoDoc GitHub license

About

  • Simple, Tiny, Flexible.
  • No reflect package.
  • No Mutex usages.
  • Any method naming allowed.
  • Compliance with JSON-RPC 2.0.

Install

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

Usage

Basic
package main

import (
	"flag"
	"log"
	"net/http"

	"github.com/lillilli/jsonrpc"
)

func main() {
	address := flag.String("address", ":65534", "")

	s := jsonrpc.NewServer()
	s.Handle("getHealthStatus", healthHandler)

	http.Handle("/jsonrpc/", s.Handler())
	log.Fatal(http.ListenAndServe(*address, nil))
}

func healthHandler(w jsonrpc.ResponseWriter, r *jsonrpc.Request) error {
	w.SetResponse("ok")
	return nil
}
Advanced
package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"

	"github.com/lillilli/jsonrpc"
)

func main() {
	address := flag.String("address", ":65534", "")

	s := jsonrpc.NewServer()
	s.Handle("getHealthStatus", healthHandler)

	http.Handle("/", s.Handler())
	log.Fatal(http.ListenAndServe(*address, nil))
}

type healthParams struct {
	Message string `json:"message"`
}

func healthHandler(w jsonrpc.ResponseWriter, r *jsonrpc.Request) error {
	params := new(healthParams)

	// return parse error with code -32700
	if err := r.Unmarshal(params); err != nil {
		return err
	}

	// will return "error": {"code": -32602, "message": "Internal error", "data": "message must be provided"}
	if params.Message == "" {
		w.SetInvalidRequestParamsError("message must be provided")
		return nil
	}

	// will return "error": {"code": -32603, "message": "Internal error", "data": "message too short"}
	if len(params.Message) < 2 {
		w.SetErrorData("message too short")
		return nil
	}

	w.SetResponse(fmt.Sprintf("ok, %s", params.Message))
	return nil
}

Examples

Invoke the Echo method
POST / HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/jso

{
  "jsonrpc": "2.0",
  "method": "getHealthStatus",
  "params": {
    "name": "John Doe"
  },
  "id": "123"
}

HTTP/1.1 200 OK

Content-Length: 43
Content-Type: application/json
Date: Wed, 27 Feb 2019 10:10:57 GMT

{
  "jsonrpc": "2.0",
  "result": "ok",
  "id": "123"
}
Invoke the notification
POST / HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/jso

{
  "jsonrpc": "2.0",
  "method": "getHealthStatus",
  "params": {
    "name": "John Doe"
  }
}

License

Released under the MIT License.

Documentation

Index

Constants

View Source
const (
	// Version is JSON-RPC 2.0 version string.
	Version = "2.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(w ResponseWriter, r *Request) error

Handler represents jsonrpc handler.

if your handler return any error, and you don`t use SetInvalidRequestParamsError() response will have code -32603 and message "Internal error"

if you use SetInvalidRequestParamsError() response will have code -32602 and message "Invalid params"

type Request

type Request struct {
	Version string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params"`
	ID      interface{}     `json:"id"`
	// contains filtered or unexported fields
}

Request represents a JSON-RPC request received by the server.

func (*Request) Unmarshal

func (r *Request) Unmarshal(v interface{}) error

Unmarshal unmarshal req params to specified structure. Unmarshal will set parse error if unmarshaling failed.

type ResponseWriter

type ResponseWriter interface {
	// SetResponse set response result.
	SetResponse(v interface{})

	// SetErrorData set response error data.
	// Error will have -32603 status code and message equal to "Internal error".
	SetErrorData(v interface{})

	// SetInvalidRequestParamsError set response error to invalid req params.
	// Error will have -32602 status code and message equal to "Invalid params".
	SetInvalidRequestParamsError(data interface{})
}

ResponseWriter represents JSON-RPC response writer interface.

type Server

type Server interface {
	// Handle set up method handler.
	Handle(method string, handler Handler)

	// Handler return standart net/http handler.
	Handler() http.Handler
}

Server represents jsonrpc server interface.

func NewServer

func NewServer() Server

NewServer returns new jsonrpc server instance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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