Documentation
¶
Overview ¶
Package errors provides a way to return detailed information for an rpc request error. The error is normally JSON encoded.
Index ¶
- Variables
- func BadRequest(id, format string, a ...any) error
- func Conflict(id, format string, a ...any) error
- func Forbidden(id, format string, a ...any) error
- 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 IdleTimeout(timeout time.Duration) Option
- func KeyFile(keyFile string) Option
- func MaxHeaderBytes(bytes int) Option
- func ReadTimeout(timeout time.Duration) Option
- func ServerOpts(s []server.Option) Option
- func SetRoute(s Route) Option
- func TLS(enable bool) Option
- func TimeOut(s time.Duration) Option
- func WriteTimeout(timeout time.Duration) Option
- type Options
- type Pair
- type Request
- type Response
- type Route
- type Service
Constants ¶
This section is empty.
Variables ¶
View Source
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, errors.New(err.Error()) } return &Service{SrvSession: session, Hander: r.URL.Path}, err }
DefaultRoute 默认路由规则
Functions ¶
func BadRequest ¶
BadRequest generates a 400 error.
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:"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
Route Route
TLS bool
CertFile string
KeyFile string
TimeOut time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
MaxHeaderBytes int
Opts []server.Option // 用来控制Module属性的
}
Options 网关配置项
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 {
// URL.Path
Hander string
// node
SrvSession app.IServerSession
}
Service represents an API service
Click to show internal directories.
Click to hide internal directories.