jsonrpc

package
v2.0.80 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: MIT Imports: 18 Imported by: 2

Documentation

Index

Constants

View Source
const (
	APP_NAME = "jsonrpc"
)

Variables

View Source
var (
	ErrProtocalError          = exception.NewApiException(32600, "Invalid Request").WithHttpCode(400)
	ErrMethodNotFound         = exception.NewApiException(32601, "Method not found").WithHttpCode(404)
	ErrInvalidParams          = exception.NewApiException(32602, "Invalida Params").WithHttpCode(400)
	ErrInvalidMethodSignature = exception.NewApiException(32603, "invalid method signature").WithHttpCode(400)
)

Functions

func Priority

func Priority() int

func RegisterService added in v2.0.79

func RegisterService(service any)

注册结构体方法(自动发现以 RPC 开头的方法)

func Registry added in v2.0.79

func Registry(methodName string, handler HandlerFunc)

1. 把业务 注册给RPC

func RootRouter

func RootRouter() *restful.Container

Types

type HandlerFunc added in v2.0.79

type HandlerFunc func(ctx context.Context, params any) (any, error)

RPC 方法处理器类型

type JsonRpc

type JsonRpc struct {
	ioc.ObjectImpl

	// 是否开启HTTP Server, 默认会根据是否有注册得有API对象来自动开启
	Enable *bool `json:"enable" yaml:"enable" toml:"enable" env:"ENABLE"`
	// HTTP服务Host
	Host string `json:"host" yaml:"host" toml:"host" env:"HOST"`
	// HTTP服务端口
	Port int `json:"port" yaml:"port" toml:"port" env:"PORT"`
	// API接口前缀
	PathPrefix string `json:"path_prefix" yaml:"path_prefix" toml:"path_prefix" env:"PATH_PREFIX"`
	// 开启Trace
	Trace bool `toml:"trace" json:"trace" yaml:"trace" env:"TRACE"`
	// 访问日志
	AccessLog bool `toml:"access_log" json:"access_log" yaml:"access_log" env:"ACCESS_LOG"`

	EnableSSL bool   `json:"enable_ssl" yaml:"enable_ssl" toml:"enable_ssl" env:"ENABLE_SSL"`
	CertFile  string `json:"cert_file" yaml:"cert_file" toml:"cert_file" env:"CERT_FILE"`
	KeyFile   string `json:"key_file" yaml:"key_file" toml:"key_file" env:"KEY_FILE"`

	Container *restful.Container
	// contains filtered or unexported fields
}

func (*JsonRpc) Addr

func (h *JsonRpc) Addr() string

func (*JsonRpc) HTTPPrefix

func (h *JsonRpc) HTTPPrefix() string

func (*JsonRpc) HandleRequest added in v2.0.79

func (j *JsonRpc) HandleRequest(r *restful.Request, w *restful.Response)

处理 JSON-RPC 请求

func (*JsonRpc) Init

func (j *JsonRpc) Init() error

func (*JsonRpc) Name

func (j *JsonRpc) Name() string

func (*JsonRpc) PrintMethods added in v2.0.79

func (j *JsonRpc) PrintMethods()

打印所有注册的方法信息

func (*JsonRpc) Priority

func (j *JsonRpc) Priority() int

func (*JsonRpc) RPCURL

func (h *JsonRpc) RPCURL() string

type MethodInfo added in v2.0.79

type MethodInfo struct {
	Name      string       // 方法名
	Handler   HandlerFunc  // 处理器函数
	FuncName  string       // 原始函数名
	ParamType reflect.Type // 参数类型
}

方法信息结构

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params"`
	ID      any             `json:"id"`
}

JSON-RPC 2.0 请求结构

type Response

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

JSON-RPC 2.0 响应结构

Jump to

Keyboard shortcuts

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