jsonrpc

package
v0.3.17 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CorsSettingsForm = forms.Form{
	ErrorMsg: "invalid data encountered in the CORS settings form",
	Fields: []forms.Field{
		{
			Name: "allowed_hosts",
			Validators: []forms.Validator{
				forms.IsOptional{Default: []string{}},
				forms.IsStringList{},
			},
		},
		{
			Name: "allowed_headers",
			Validators: []forms.Validator{
				forms.IsOptional{Default: []string{}},
				forms.IsStringList{},
			},
		},
		{
			Name: "allowed_methods",
			Validators: []forms.Validator{
				forms.IsOptional{Default: []string{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"}},
				forms.IsList{
					Validators: []forms.Validator{
						forms.IsIn{
							Choices: []interface{}{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"},
						},
					},
				},
				forms.IsStringList{},
			},
		},
	},
}
View Source
var JSONRPCClientSettingsForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "endpoint",
			Validators: []forms.Validator{
				forms.IsOptional{},
				forms.IsString{},
			},
		},
		{
			Name: "proxy_url",
			Validators: []forms.Validator{
				forms.IsOptional{},
				forms.IsString{},
			},
		},
		{
			Name: "local",
			Validators: []forms.Validator{
				forms.IsOptional{Default: true},
				forms.IsBoolean{},
			},
		},
		{
			Name: "tls",
			Validators: []forms.Validator{
				forms.IsOptional{},
				forms.IsStringMap{
					Form: &tls.TLSSettingsForm,
				},
			},
		},
	},
}
View Source
var JSONRPCRequestForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "jsonrpc",
			Validators: []forms.Validator{
				forms.IsString{},
				forms.IsIn{

					Choices: []interface{}{"2.0"},
				},
			},
		},
		{
			Name: "method",
			Validators: []forms.Validator{
				forms.IsString{
					MinLength: 1,
					MaxLength: 100,
				},
			},
		},
		{
			Name: "params",
			Validators: []forms.Validator{

				forms.IsStringMap{},
			},
		},
		{
			Name: "id",
			Validators: []forms.Validator{

				forms.IsOptional{},

				forms.Or{
					Options: [][]forms.Validator{
						{

							forms.IsString{
								MinLength: 0,
								MaxLength: 1024,
							},
						},
						{

							forms.IsInteger{
								HasMin: true,
								HasMax: true,
								Min:    -2147483648,
								Max:    2147483647,
							},
						},
					},
				},
			},
		},
	},
}
View Source
var JSONRPCServerSettingsForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "cors",
			Validators: []forms.Validator{
				forms.IsOptional{},
				forms.IsStringMap{
					Form: &CorsSettingsForm,
				},
			},
		},
		{
			Name: "bind_address",
			Validators: []forms.Validator{
				forms.IsString{},
			},
		},
		net.TCPRateLimitsField,
		{
			Name: "path",
			Validators: []forms.Validator{
				forms.IsOptional{Default: "/jsonrpc"},
				forms.IsString{},
			},
		},
		{
			Name: "tls",
			Validators: []forms.Validator{
				forms.IsOptional{},
				forms.IsStringMap{
					Form: &tls.TLSSettingsForm,
				},
			},
		},
	},
}

Functions

func Cors

func Cors(settings *CorsSettings, defaultRoute bool) http.Handler

func CorsFromEverywhere

func CorsFromEverywhere(settings *CorsSettings) http.Handler

func ExtractJSONRequest

func ExtractJSONRequest(c *http.Context)

extracts the request data from

func JSONRPC

func JSONRPC(handler Handler) http.Handler

func NotFound

func NotFound(c *http.Context)

Types

type Client

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

func MakeClient

func MakeClient(settings *JSONRPCClientSettings) *Client

func (*Client) Call

func (c *Client) Call(request *Request) (*Response, error)

func (*Client) SetEndpoint

func (c *Client) SetEndpoint(endpoint string)

func (*Client) SetServerName

func (c *Client) SetServerName(serverName string)

type Coercer

type Coercer func(interface{}) error

type Context

type Context struct {
	Request     *Request
	HTTPContext *http.Context
}

func (*Context) Acknowledge

func (c *Context) Acknowledge() *Response

func (*Context) Error

func (c *Context) Error(code int64, message string, data interface{}) *Response

func (*Context) InternalError

func (c *Context) InternalError() *Response

func (*Context) InvalidParams

func (c *Context) InvalidParams(err error) *Response

func (*Context) MethodNotFound

func (c *Context) MethodNotFound() *Response

func (*Context) Nil

func (c *Context) Nil() *Response

func (*Context) NotFound

func (c *Context) NotFound() *Response

func (*Context) Result

func (c *Context) Result(data interface{}) *Response

type CorsSettings

type CorsSettings struct {
	AllowedHeaders []string `json:"allowed_headers"`
	AllowedHosts   []string `json:"allowed_hosts"`
	AllowedMethods []string `json:"allowed_methods"`
}

type Error

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

func MakeError

func MakeError(code int64, message string, data interface{}) *Error

type Handler

type Handler func(*Context) *Response

func MethodsHandler

func MethodsHandler(methods map[string]*Method) (Handler, error)

A more advanced handler that performs form-based input sanization and coerces the result into a user-provided data structure via reflection.

type JSONRPCClientSettings

type JSONRPCClientSettings struct {
	TLS      *tls.TLSSettings `json:"tls"`
	Endpoint string           `json:"endpoint"`
	ProxyUrl string           `json:"proxy_url"`
	Local    bool             `json:"local"`
}

Settings for the JSON-RPC server

type JSONRPCServer

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

func MakeJSONRPCServer

func MakeJSONRPCServer(settings *JSONRPCServerSettings, handler Handler) (*JSONRPCServer, error)

func (*JSONRPCServer) HTTPServer

func (s *JSONRPCServer) HTTPServer() *http.HTTPServer

func (*JSONRPCServer) Start

func (s *JSONRPCServer) Start() error

func (*JSONRPCServer) Stop

func (s *JSONRPCServer) Stop() error

type JSONRPCServerSettings

type JSONRPCServerSettings struct {
	Cors          *CorsSettings    `json:"cors"`
	TLS           *tls.TLSSettings `json:"tls"`
	BindAddress   string           `json:"bind_address"`
	TCPRateLimits []*net.RateLimit `json:"tcp_rate_limits"`
	Path          string           `json:"path"`
}

Settings for the JSON-RPC server

type Method

type Method struct {
	Form    *forms.Form
	Handler interface{}
}

type Request

type Request struct {
	JSONRPC string                 `json:"jsonrpc"`
	Method  string                 `json:"method"`
	Params  map[string]interface{} `json:"params"`
	ID      string                 `json:"id"`
}

we always convert incoming IDs to strings

func MakeRequest

func MakeRequest(method, id string, params map[string]interface{}) *Request

func (*Request) FromHyperRequest

func (r *Request) FromHyperRequest(request *hyper.Request)

type Response

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

func FromHyperResponse

func FromHyperResponse(response *hyper.Response) *Response

func (*Response) ToHyperResponse

func (r *Response) ToHyperResponse() *hyper.Response

Jump to

Keyboard shortcuts

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