Documentation ¶
Index ¶
- Constants
- func ForwardToHost(w http.ResponseWriter, r *http.Request, host string, errLog *log.Logger, ...) error
- func ForwardToHostComplexly(w http.ResponseWriter, r *http.Request, host string, errLog *log.Logger, ...) error
- func ForwardToSingleHost(w http.ResponseWriter, r *http.Request, url string, errLog *log.Logger) (err error)
- func IsInterruptedError(err error) bool
- func IsProxyDialError(err error) bool
- func NewDirector(targetHost string, options ...DirectorOption) (director func(req *http.Request), err error)
- func NewSingleHostDirector(target *url.URL) (director func(req *http.Request), err error)
- type Backend
- type DirectorOption
- type LoadBalancer
- type ReverseProxy
- func NewReverseProxy(director func(*http.Request), transport http.RoundTripper, ...) (*ReverseProxy, error)
- func NewSingleHostReverseProxy(u *url.URL, errLog *log.Logger) (*ReverseProxy, error)
- func NewSingleHostReverseProxyWithUrl(urlStr string, errLog *log.Logger) (*ReverseProxy, error)
- func WrapToReverseProxy(reverseProxy *httputil.ReverseProxy) (*ReverseProxy, error)
Constants ¶
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 ¶
IsInterruptedError 是否是被中断error?(e.g. 请求被取消...)
PS: 如果返回值为true,说明请求已经结束了,无需再响应内容给http客户端了.
func IsProxyDialError ¶
IsProxyDialError 代理请求返回的error,是否是因为dial目标地址失败?
func NewDirector ¶
func NewDirector(targetHost string, options ...DirectorOption) (director func(req *http.Request), err error)
NewDirector
@param targetHost hostname || hostname:port
Types ¶
type Backend ¶ added in v3.0.908
func NewBackend ¶ added in v3.0.908
func NewBackend(rp *httputil.ReverseProxy) (*Backend, error)
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
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 ¶
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的字段,但不希望修改方法体外的.