forwardKit

package
v3.0.917 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyForwardError = "chimera-forward-error"
)

Variables

This section is empty.

Functions

func ForwardToHost

func ForwardToHost(w http.ResponseWriter, r *http.Request, host string, errLog *log.Logger, options ...DirectorOption) error

ForwardToHost 代理请求.

PS: 代理请求失败时,建议返回状态码502(http.StatusBadGateway, 网关错误).

@param host e.g."127.0.0.1:80" @param errLog 可以为nil(即无输出,但不推荐这么干)

func ForwardToHostComplexly

func ForwardToHostComplexly(w http.ResponseWriter, r *http.Request, host string, errLog *log.Logger,
	transport http.RoundTripper, modifyResponse func(*http.Response) error, options ...DirectorOption) error

ForwardToHostComplexly 代理请求.

PS: 代理请求失败时,建议返回状态码502(http.StatusBadGateway, 网关错误).

@param host e.g."127.0.0.1:80" @param errLog 可以为nil(即无输出,但不推荐这么干) @param transport 可以为nil @param modifyResponse 可以为nil

func ForwardToSingleHost

func ForwardToSingleHost(w http.ResponseWriter, r *http.Request, url string, errLog *log.Logger) (err error)

ForwardToSingleHost 代理请求(请求的scheme不会变).

PS: 代理请求失败时,建议返回状态码502(http.StatusBadGateway, 网关错误).

@param errLogger 可以为nil(即无输出,但不推荐这么干) @param url 目标url

e.g. 	"http://127.0.0.1:8000": 将请求转发给"http://127.0.0.1:8000",请求路由不变
e.g.1 	"http://127.0.0.1:8000/a": 将请求转发给"http://127.0.0.1:8000",请求路由的最前面加上"/a"

func IsInterruptedError

func IsInterruptedError(err error) bool

IsInterruptedError 是否是被中断error?(e.g. 请求被取消...)

PS: 如果返回值为true,说明请求已经结束了,无需再响应内容给http客户端了.

func IsProxyDialError

func IsProxyDialError(err error) bool

IsProxyDialError 代理请求返回的error,是否是因为dial目标地址失败?

func NewDirector

func NewDirector(targetHost string, options ...DirectorOption) (director func(req *http.Request), err error)

NewDirector

@param targetHost hostname || hostname:port

func NewSingleHostDirector

func NewSingleHostDirector(target *url.URL) (director func(req *http.Request), err error)

NewSingleHostDirector

TODO: 参考了 httputil.NewSingleHostReverseProxy,后续要实时更新(Golang版本升级).

Types

type Backend added in v3.0.908

type Backend struct {
	mutexKit.RWMutex

	Id string

	Access bool

	ReverseProxy *httputil.ReverseProxy
}

func NewBackend added in v3.0.908

func NewBackend(rp *httputil.ReverseProxy) (*Backend, error)

func (*Backend) IsAccess added in v3.0.908

func (be *Backend) IsAccess() (access bool)

func (*Backend) SetAccess added in v3.0.908

func (be *Backend) SetAccess(flag bool)

type DirectorOption

type DirectorOption func(opts *directorOptions)

func WithExtraQueryParams

func WithExtraQueryParams(extraQueryParams map[string][]string) DirectorOption

func WithOverrideQueryParams

func WithOverrideQueryParams(overrideQueryParams map[string][]string) DirectorOption

func WithRequestUrlPath

func WithRequestUrlPath(requestUrlPath string) DirectorOption

func WithScheme

func WithScheme(scheme string) DirectorOption

type LoadBalancer added in v3.0.908

type LoadBalancer struct {
	mutexKit.RWMutex
	// contains filtered or unexported fields
}

func (*LoadBalancer) AddBackend added in v3.0.908

func (lb *LoadBalancer) AddBackend(backend *Backend)

func (*LoadBalancer) Handle added in v3.0.908

func (lb *LoadBalancer) Handle(w http.ResponseWriter, t *http.Request)

Handle 负载均衡http请求.

type ReverseProxy added in v3.0.903

type ReverseProxy struct {
	// !!!: 此处不能是 *httputil.ReverseProxy,因为会在 Forward 方法体内修改receiver的字段,但不希望修改方法体外的.
	httputil.ReverseProxy
}

func NewReverseProxy added in v3.0.903

func NewReverseProxy(director func(*http.Request), transport http.RoundTripper, modifyResponse func(*http.Response) error, errLog *log.Logger, errHandler func(http.ResponseWriter, *http.Request, error)) (*ReverseProxy, error)

NewReverseProxy

PS: 对于 httputil.ReverseProxy 结构体,Rewrite 和 Director 只能有一个非nil.

@param director 不能为nil!!! @param transport 可以为nil @param modifyResponse 可以为nil @param errLog 可以为nil(即无输出,但不推荐这么干) @param errHandler 可以为nil

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(u *url.URL, errLog *log.Logger) (*ReverseProxy, error)

NewSingleHostReverseProxy

@param target 不能为nil,否则会panic @param errLog 可以为nil(即无输出,但不推荐这么干) @return !!!: Transport、ModifyResponse、ErrorHandler 等字段为nil

func NewSingleHostReverseProxyWithUrl

func NewSingleHostReverseProxyWithUrl(urlStr string, errLog *log.Logger) (*ReverseProxy, error)

NewSingleHostReverseProxyWithUrl

@param urlStr 目标url

e.g. 	"http://127.0.0.1:8000": 将请求转发给"http://127.0.0.1:8000",请求路由不变
e.g.1 	"http://127.0.0.1:8000/a": 将请求转发给"http://127.0.0.1:8000",请求路由的最前面加上"/a"

@param errLog 可以为nil(即无输出,但不推荐这么干) @return !!!: Transport、ModifyResponse、ErrorHandler 等字段为nil

func WrapToReverseProxy added in v3.0.908

func WrapToReverseProxy(reverseProxy *httputil.ReverseProxy) (*ReverseProxy, error)

WrapToReverseProxy *httputil.ReverseProxy => *forwardKit.ReverseProxy

func (ReverseProxy) Forward added in v3.0.903

func (rp ReverseProxy) Forward(w http.ResponseWriter, r *http.Request) (err error)

Forward 请求转发(代理请求; proxy).

!!!: 此方法的receiver不能为指针类型,因为会在方法体内修改receiver的字段,但不希望修改方法体外的.

Jump to

Keyboard shortcuts

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