mirror of
https://github.com/labstack/echo.git
synced 2024-12-18 16:20:53 +02:00
6463bcb190
Co-authored-by: Peter C <petoc@users.noreply.github.com>
26 lines
691 B
Go
26 lines
691 B
Go
// +build go1.11
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func proxyHTTP(tgt *ProxyTarget, c echo.Context, config ProxyConfig) http.Handler {
|
|
proxy := httputil.NewSingleHostReverseProxy(tgt.URL)
|
|
proxy.ErrorHandler = func(resp http.ResponseWriter, req *http.Request, err error) {
|
|
desc := tgt.URL.String()
|
|
if tgt.Name != "" {
|
|
desc = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
|
|
}
|
|
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err)))
|
|
}
|
|
proxy.Transport = config.Transport
|
|
proxy.ModifyResponse = config.ModifyResponse
|
|
return proxy
|
|
}
|