diff options
Diffstat (limited to 'vendor/github.com/labstack/echo/middleware/proxy.go')
-rw-r--r-- | vendor/github.com/labstack/echo/middleware/proxy.go | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/vendor/github.com/labstack/echo/middleware/proxy.go b/vendor/github.com/labstack/echo/middleware/proxy.go index 7eb24abf..4f55f39d 100644 --- a/vendor/github.com/labstack/echo/middleware/proxy.go +++ b/vendor/github.com/labstack/echo/middleware/proxy.go @@ -1,7 +1,6 @@ package middleware import ( - "errors" "fmt" "io" "math/rand" @@ -54,35 +53,38 @@ type ( } ) +var ( + // DefaultProxyConfig is the default Proxy middleware config. + DefaultProxyConfig = ProxyConfig{ + Skipper: DefaultSkipper, + } +) + func proxyHTTP(t *ProxyTarget) http.Handler { return httputil.NewSingleHostReverseProxy(t.URL) } func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - h, ok := w.(http.Hijacker) - if !ok { - c.Error(errors.New("proxy raw, not a hijacker")) - return - } - in, _, err := h.Hijack() + in, _, err := c.Response().Hijack() if err != nil { - c.Error(fmt.Errorf("proxy raw, hijack error=%v, url=%s", r.URL, err)) + c.Error(fmt.Errorf("proxy raw, hijack error=%v, url=%s", t.URL, err)) return } defer in.Close() out, err := net.Dial("tcp", t.URL.Host) if err != nil { - he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", r.URL, err)) + he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", t.URL, err)) c.Error(he) return } defer out.Close() + // Write header err = r.Write(out) if err != nil { - he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request copy error=%v, url=%s", r.URL, err)) + he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request header copy error=%v, url=%s", t.URL, err)) c.Error(he) return } @@ -97,7 +99,7 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler { go cp(in, out) err = <-errc if err != nil && err != io.EOF { - c.Logger().Errorf("proxy raw, error=%v, url=%s", r.URL, err) + c.Logger().Errorf("proxy raw, copy body error=%v, url=%s", t.URL, err) } }) } @@ -118,8 +120,18 @@ func (r *RoundRobinBalancer) Next() *ProxyTarget { return t } -// Proxy returns an HTTP/WebSocket reverse proxy middleware. -func Proxy(config ProxyConfig) echo.MiddlewareFunc { +// Proxy returns a Proxy middleware. +// +// Proxy middleware forwards the request to upstream server using a configured load balancing technique. +func Proxy(balancer ProxyBalancer) echo.MiddlewareFunc { + c := DefaultProxyConfig + c.Balancer = balancer + return ProxyWithConfig(c) +} + +// ProxyWithConfig returns a Proxy middleware with config. +// See: `Proxy()` +func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc { // Defaults if config.Skipper == nil { config.Skipper = DefaultLoggerConfig.Skipper @@ -130,6 +142,10 @@ func Proxy(config ProxyConfig) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) (err error) { + if config.Skipper(c) { + return next(c) + } + req := c.Request() res := c.Response() tgt := config.Balancer.Next() |