httpgateway

package
v1.4.14 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package httpgateway provides an http-rpc handler which provides the entire http request over rpc

Package httpgateway 网关配置

Index

Constants

This section is empty.

Variables

View Source
var DefaultRoute = func(app module.App, 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")
	}
	server := handers[1]
	if server == "" {
		return nil, errors.New("server is nil")
	}
	session, err := app.GetRouteServer(server,
		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 NewHandler

func NewHandler(app module.App, opts ...Option) http.Handler

NewHandler 创建网关

Types

type APIHandler added in v1.3.6

type APIHandler struct {
	Opts Options
	App  module.App
}

APIHandler 网关handler

func (*APIHandler) ServeHTTP added in v1.3.6

func (a *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

API handler is the default handler which takes api.Request and returns api.Response

type Option

type Option func(*Options)

Option 配置

func SetRoute

func SetRoute(s Route) Option

SetRoute 设置路由器

func TimeOut

func TimeOut(s time.Duration) Option

TimeOut 设置网关超时时间

type Options

type Options struct {
	TimeOut time.Duration
	Route   Route
}

Options 网关配置项

func NewOptions

func NewOptions(app module.App, opts ...Option) Options

NewOptions 创建配置

type Route

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

Route 路由器定义

type Service

type Service struct {
	// hander
	Hander string
	// node
	SrvSession module.ServerSession
}

Service represents an API service

Directories

Path Synopsis
Package errors provides a way to return detailed information for an rpc request error.
Package errors provides a way to return detailed information for an rpc request error.

Jump to

Keyboard shortcuts

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