Documentation
¶
Overview ¶
Package errors provides a way to return detailed information for an rpc request error. The error is normally JSON encoded.
Index ¶
- Constants
- Variables
- func BadRequest(id, format string, a ...any) error
- func Conflict(id, format string, a ...any) error
- func ContextValHeader(ctx context.Context) map[string]*Pair
- func Forbidden(id, format string, a ...any) error
- func HttpHead(head map[string]*Pair) mqrpc.IMarshaler
- func InternalServerError(id, format string, a ...any) error
- func MethodNotAllowed(id, format string, a ...any) error
- func NewError(id, detail string, code int32) error
- func NotFound(id, format string, a ...any) error
- func Timeout(id, format string, a ...any) error
- func Unauthorized(id, format string, a ...any) error
- type Error
- type Event
- type Option
- func Addr(addr string) Option
- func CertFile(certFile string) Option
- func DebugKey(key string) Option
- func EncryptKey(key string) Option
- func IdleTimeout(timeout time.Duration) Option
- func KeyFile(keyFile string) Option
- func MaxHeaderBytes(bytes int) Option
- func ReadTimeout(timeout time.Duration) Option
- func Route(s Router) Option
- func ServerOpts(s []server.Option) Option
- func TLS(enable bool) Option
- func Transfers(t Transfer) Option
- func WriteTimeout(timeout time.Duration) Option
- type Options
- type Pair
- type Request
- type Response
- type Router
- type Service
- type Transfer
Constants ¶
const ( // 服务器配置 SettingKeyAddr = "addr" // 监听地址 // tls SettingKeyTLS = "tls" // 是否启用TLS SettingKeyCertFile = "tls_cert_file" // 证书文件路径 SettingKeyKeyFile = "tls_key_file" // 私钥文件路径 SettingKeyReadTimeout = "read_timeout" // 读取超时(秒) SettingKeyWriteTimeout = "write_timeout" // 写入超时(秒) SettingKeyIdleTimeout = "idle_timeout" // 空闲超时(秒) SettingKeyMaxHeaderBytes = "max_header_bytes" // 最大头部字节数 // 安全配置 SettingKeyDebugKey = "debug_key" // 调试密钥 SettingKeyEncryptKey = "encrypt_key" // 加密密钥 )
配置Setting键常量定义
const ( RPC_CONTEXT_KEY_HEADER = "rtx_header" // 定义需要RPC传输http.Header的ContextKey HTTP_HEAD_KEY_DEBUG_KEY = "t_debug_key" // client指定header的此key可以消息不用加密 )
Variables ¶
var DefaultRoute = func(r *http.Request) (*Service, error) { if r.URL.Path == "" { return nil, errors.New("path is nil") } handers := strings.Split(r.URL.Path, "/") if len(handers) < 2 { return nil, errors.New("path is not /[server]/path") } service := handers[1] if service == "" { return nil, errors.New("module server is nil") } session, err := app.App().GetRouteServer(service, selector.WithStrategy(func(services []*registry.Service) selector.Next { var nodes []*registry.Node for _, service := range services { for _, node := range service.Nodes { nodes = append(nodes, node) } } var mtx sync.Mutex return func() (*registry.Node, error) { mtx.Lock() defer mtx.Unlock() if len(nodes) == 0 { return nil, fmt.Errorf("no node") } index := rand.Intn(int(len(nodes))) return nodes[index], nil } }), ) if err != nil { return nil, err } return &Service{Server: session, Topic: r.URL.Path}, err }
DefaultRoute 默认路由规则
var DefaultTransfe = func(service *Service, req *Request, rsp *Response) error { return mqrpc.MsgPack(rsp, mqrpc.RpcResult(service.Server.GetRPC().Call(context.TODO(), service.Topic, req))) }
DefaultTransfe 默认转发规则
Functions ¶
func BadRequest ¶
BadRequest generates a 400 error.
func ContextValHeader ¶
get Header from context
func InternalServerError ¶
InternalServerError generates a 500 error.
func MethodNotAllowed ¶
MethodNotAllowed generates a 405 error.
func Unauthorized ¶
Unauthorized generates a 401 error.
Types ¶
type Error ¶
type Error struct {
Id string `json:"id"`
Code int32 `json:"code"`
Detail string `json:"err"` // old : detail
Status string `json:"status"`
}
Error implements the error interface.
func ParseError ¶
ParseError tries to parse a JSON string into an error. If that fails, it will set the given string as the error detail.
type Event ¶
type Event struct {
Name string `msgpack:"name" json:"name"`
Id string `msgpack:"id" json:"id"`
Timestamp int64 `msgpack:"timestamp" json:"timestamp"`
Header map[string]*Pair `msgpack:"header,omitempty" json:"header,omitempty"`
Data string `msgpack:"data,omitempty" json:"data,omitempty"`
}
A HTTP event as RPC, Forwarded by the event handler
type Options ¶
type Options struct {
Addr string // Settings["Addr"]
Route Router // 控制如何选择rpc服务
Transfer Transfer // 控制如何处理api请求
TLS bool
CertFile string
KeyFile string
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
DebugKey string // 调试用(可不用加密调试)(Settings["DebugKey"])
EncryptKey string // 消息包加密key(Settings["EncryptKey"])(must 16, 24 or 32 bytes)
Opts []server.Option // 用来控制Module属性的
}
Options 网关配置项
type Pair ¶
type Pair struct {
Key string `msgpack:"key" json:"key"`
Values []string `msgpack:"values,omitempty" json:"values,omitempty"`
}
header's value
type Request ¶
type Request struct {
Method string `msgpack:"method" json:"method"`
Path string `msgpack:"path" json:"path"`
Header map[string]*Pair `msgpack:"header,omitempty" json:"header,omitempty"`
Get map[string]*Pair `msgpack:"get,omitempty" json:"get,omitempty"`
Post map[string]*Pair `msgpack:"post,omitempty" json:"post,omitempty"`
Body string `msgpack:"body,omitempty" json:"body,omitempty"` // raw request body; if not application/x-www-form-urlencoded
Url string `msgpack:"url,omitempty" json:"url,omitempty"`
}
A HTTP request as RPC, Forward by the api handler
type Response ¶
type Response struct {
StatusCode int32 `msgpack:"status_code" json:"status_code"`
Header map[string]*Pair `msgpack:"header,omitempty" json:"header,omitempty"`
Body string `msgpack:"body,omitempty" json:"body,omitempty"`
}
A HTTP response as RPC, Expected response for the api handler
type Service ¶
type Service struct {
// topic
Topic string // msg_id
// module server
Server app.IModuleServerSession
}
Service represents an API service