jsonrpc

package
v2.0.86 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 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

func SetAuther added in v2.0.86

func SetAuther(a Auther)

Types

type AuthRequest added in v2.0.84

type AuthRequest struct {
	Header *http.Header `json:"header"`
	Method string       `json:"method"`
}

type Auther added in v2.0.84

type Auther interface {
	// RPC认证接口, 返回认证信息, 携带在RpcContext中, 可以通过GetRpcContext获取
	Auth(context.Context, *AuthRequest) (authInfo any, err error)
}

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

	// 鉴权器
	ClientId     string `json:"client_id" yaml:"client_id" toml:"client_id" env:"CLIENT_ID"`
	ClientSecret string `json:"client_secret" yaml:"client_secret" toml:"client_secret" env:"CLIENT_SECRET"`

	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 Get added in v2.0.84

func Get() *JsonRpc

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) IsEnable added in v2.0.84

func (h *JsonRpc) IsEnable() bool

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

func (*JsonRpc) Start added in v2.0.84

func (h *JsonRpc) Start(ctx context.Context)

func (*JsonRpc) Stop added in v2.0.84

func (h *JsonRpc) Stop(ctx context.Context) error

Stop 停止server

type MethodInfo added in v2.0.79

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

方法信息结构

type Request

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

JSON-RPC 2.0 请求结构

func NewRequest

func NewRequest[T any](method string, params T) *Request[T]

func (*Request[T]) SetID added in v2.0.82

func (r *Request[T]) SetID(id any) *Request[T]

type Response

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

JSON-RPC 2.0 响应结构

func NewResponse

func NewResponse[T any]() *Response[T]

func (*Response[T]) SetID added in v2.0.82

func (r *Response[T]) SetID(id any) *Response[T]

type RpcContext added in v2.0.84

type RpcContext struct {
	AuthInfo any
}

func GetRpcContext added in v2.0.84

func GetRpcContext(ctx context.Context) *RpcContext

type RpcContextKey added in v2.0.84

type RpcContextKey struct{}

Jump to

Keyboard shortcuts

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