booze

package module
v0.0.0-...-5d9bd77 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2018 License: MIT Imports: 10 Imported by: 0

README

booze 🍻

Golang Websocket JSON-RPC 2.0 implementation

Usage

import (
	"context"
	"log"
	"net/http"
	"github.com/snaffi/booze"
)


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

func someRpcHandler(ctx context.Context, payload *booze.Payload) (interface{}, *booze.Error) {
	var object SomeStruct
	err := payload.Unmarshal(&object)
	if err != nil {
		e := &InvalidRequest
		e.Data = err
		return nil, e
	}
	return &object, nil
}

func main() {
	rpcHandler := booze.NewRPCHandler()

	rpcHandler.Register("some_handler", someRpcHandler)

	http.Handle("/ws_rpc", rpcHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Input data format


{
    "id": "1",
    "method": "some_handler",
    "params": {
        "message": "hi"
    }
    "jsonrpc":"2.0",
}

Output data format


{
    "id": "1",
    "result": {
        "message": "hi"
    },
    "jsonrpc":"2.0"
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ParseError       = Error{Code: -32700, Message: "parse error"}
	SystemError      = Error{Code: -32400, Message: "system error"}
	InternalError    = Error{Code: -32603, Message: "internal error"}
	InvalidParams    = Error{Code: -32602, Message: "invalid params"}
	TransportError   = Error{Code: -32300, Message: "transport error"}
	InvalidRequest   = Error{Code: -32600, Message: "invalid request"}
	MethodNotFound   = Error{Code: -32601, Message: "method not found"}
	ApplicationError = Error{Code: -32500, Message: "application error"}
)
View Source
var (
	HandlerAlreadyExist = errors.New("handler already exist")
)

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    int         `json:"code"`
	Data    interface{} `json:"data,omitempty"`
	Message string      `json:"message"`
}

func (Error) Error

func (e Error) Error() string

type Payload

type Payload struct {
	ID     string          `json:"id"`
	Method string          `json:"method"`
	Params json.RawMessage `json:"params"`
}

func (*Payload) Marshal

func (p *Payload) Marshal(v interface{}) (err error)

func (*Payload) Unmarshal

func (p *Payload) Unmarshal(v interface{}) error

type RPCHandler

type RPCHandler struct {
	// contains filtered or unexported fields
}

func NewRPCHandler

func NewRPCHandler() *RPCHandler

func (*RPCHandler) Register

func (h *RPCHandler) Register(methodName string, handlerFunc RPCHandlerFunc)

func (*RPCHandler) ServeHTTP

func (h *RPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type RPCHandlerFunc

type RPCHandlerFunc func(ctx context.Context, payload *Payload) (interface{}, *Error)

type RPCVersion20

type RPCVersion20 string

func (RPCVersion20) MarshalJSON

func (v RPCVersion20) MarshalJSON() ([]byte, error)

type Response

type Response struct {
	ID         string       `json:"id"`
	Error      *Error       `json:"error,omitempty"`
	Result     interface{}  `json:"result,omitempty"`
	RPCVersion RPCVersion20 `json:"jsonrpc"`
}

Jump to

Keyboard shortcuts

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