httpgate

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package errors provides a way to return detailed information for an rpc request error. The error is normally JSON encoded.

Index

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

func BadRequest(id, format string, a ...any) error

BadRequest generates a 400 error.

func Conflict

func Conflict(id, format string, a ...any) error

Conflict generates a 409 error.

func Forbidden

func Forbidden(id, format string, a ...any) error

Forbidden generates a 403 error.

func InternalServerError

func InternalServerError(id, format string, a ...any) error

InternalServerError generates a 500 error.

func MethodNotAllowed

func MethodNotAllowed(id, format string, a ...any) error

MethodNotAllowed generates a 405 error.

func NewError

func NewError(id, detail string, code int32) error

NewError generates a custom error.

func NotFound

func NotFound(id, format string, a ...any) error

NotFound generates a 404 error.

func Timeout

func Timeout(id, format string, a ...any) error

Timeout generates a 408 error.

func Unauthorized

func Unauthorized(id, format string, a ...any) error

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

func ParseError(err string) *Error

ParseError tries to parse a JSON string into an error. If that fails, it will set the given string as the error detail.

func (*Error) Error

func (e *Error) Error() string

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 Option

type Option func(*Options)

Option 配置

func Addr

func Addr(addr string) Option

Addr 设置监听地址

func CertFile

func CertFile(certFile string) Option

CertFile 设置证书文件路径

func IdleTimeout

func IdleTimeout(timeout time.Duration) Option

IdleTimeout 设置空闲超时

func KeyFile

func KeyFile(keyFile string) Option

KeyFile 设置私钥文件路径

func MaxHeaderBytes

func MaxHeaderBytes(bytes int) Option

MaxHeaderBytes 设置最大头部字节数

func ReadTimeout

func ReadTimeout(timeout time.Duration) Option

ReadTimeout 设置读取超时

func ServerOpts

func ServerOpts(s []server.Option) Option

ServerOpts ServerOpts

func SetRoute

func SetRoute(s Route) Option

SetRoute 设置路由器

func TLS

func TLS(enable bool) Option

TLS 设置是否启用TLS

func TimeOut

func TimeOut(s time.Duration) Option

TimeOut 设置网关超时时间

func WriteTimeout

func WriteTimeout(timeout time.Duration) Option

WriteTimeout 设置写入超时

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 网关配置项

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions 创建配置

type Pair

type Pair struct {
	Key    string   `msgpack:"key" json:"key"`
	Values []string `msgpack:"values,omitempty" json:"values,omitempty"`
}

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 Route

type Route func(r *http.Request) (*Service, error)

Route 路由器定义

type Service

type Service struct {
	// URL.Path
	Hander string
	// node
	SrvSession app.IServerSession
}

Service represents an API service

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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