Documentation ¶
Index ¶
- Constants
- func ForwardByReverseProxy(w http.ResponseWriter, r *http.Request, reverseProxy *httputil.ReverseProxy) (err error)
- 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 NewCustomReverseProxy(director func(*http.Request), transport http.RoundTripper, ...) (*httputil.ReverseProxy, error)
- func NewDirector(targetHost string, options ...DirectorOption) (director func(req *http.Request), err error)
- func NewSingleHostDirector(u *url.URL) (func(r *http.Request), error)
- func NewSingleHostReverseProxy(u *url.URL) (rp *httputil.ReverseProxy, err error)
- func NewSingleHostReverseProxyWithUrl(urlStr string) (*httputil.ReverseProxy, error)
- type DirectorOption
Constants ¶
const (
KeyForwardError = "chimera-forward-error"
)
Variables ¶
This section is empty.
Functions ¶
func ForwardByReverseProxy ¶
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 ¶
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 NewCustomReverseProxy ¶
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 NewSingleHostReverseProxy ¶
func NewSingleHostReverseProxy(u *url.URL) (rp *httputil.ReverseProxy, err error)
NewSingleHostReverseProxy
@return (1) Director字段: != nil;
(2) Transport、ModifyResponse、ErrorLog、ErrorHandler等字段: == nil.
func NewSingleHostReverseProxyWithUrl ¶
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