forwardKit

package
v3.0.926 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func ForwardByReverseProxy added in v3.0.918

func ForwardByReverseProxy(w http.ResponseWriter, r *http.Request, reverseProxy *httputil.ReverseProxy) (err error)

ForwardByReverseProxy !!!: 应该使用 此函数 而非直接使用 httputil.ReverseProxy 的 ServeHTTP 方法(因为代理失败时,要从 ErrorHandler字段 中获取error).

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

@param reverseProxy 不能为nil

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 added in v3.0.908

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 added in v3.0.904

func IsInterruptedError(err error) bool

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

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

func IsProxyDialError added in v3.0.904

func IsProxyDialError(err error) bool

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

func NewCustomReverseProxy added in v3.0.918

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

NewCustomReverseProxy 新建自定义的 *httputil.ReverseProxy 实例.

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

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

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(u *url.URL) (func(r *http.Request), error)

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(u *url.URL) (rp *httputil.ReverseProxy, err error)

NewSingleHostReverseProxy

@return (1) Director字段: != nil;

(2) Transport、ModifyResponse、ErrorLog、ErrorHandler等字段: == nil.

func NewSingleHostReverseProxyWithUrl added in v3.0.908

func NewSingleHostReverseProxyWithUrl(urlStr string) (*httputil.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"

@return (1) Director字段: != nil;

(2) Transport、ModifyResponse、ErrorLog、ErrorHandler等字段: == nil.

Types

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

Jump to

Keyboard shortcuts

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