Documentation
¶
Index ¶
- Constants
- Variables
- func Priority() int
- func RegisterService(service any)
- func Registry(methodName string, handler HandlerFunc)
- func RootRouter() *restful.Container
- func SetAuther(a Auther)
- type AuthRequest
- type Auther
- type HandlerFunc
- type JsonRpc
- func (h *JsonRpc) Addr() string
- func (h *JsonRpc) HTTPPrefix() string
- func (j *JsonRpc) HandleRequest(r *restful.Request, w *restful.Response)
- func (j *JsonRpc) Init() error
- func (h *JsonRpc) IsEnable() bool
- func (j *JsonRpc) Name() string
- func (j *JsonRpc) PrintMethods()
- func (j *JsonRpc) Priority() int
- func (h *JsonRpc) RPCURL() string
- func (h *JsonRpc) Start(ctx context.Context)
- func (h *JsonRpc) Stop(ctx context.Context) error
- type MethodInfo
- type Request
- type Response
- type RpcContext
- type RpcContextKey
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 Registry ¶ added in v2.0.79
func Registry(methodName string, handler HandlerFunc)
1. 把业务 注册给RPC
func RootRouter ¶
func RootRouter() *restful.Container
Types ¶
type AuthRequest ¶ added in v2.0.84
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
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 (*JsonRpc) HTTPPrefix ¶
func (*JsonRpc) HandleRequest ¶ added in v2.0.79
func (j *JsonRpc) HandleRequest(r *restful.Request, w *restful.Response)
处理 JSON-RPC 请求
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 ¶
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 ¶
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{}
Click to show internal directories.
Click to hide internal directories.