2018-10-13 08:10:19 +02:00
|
|
|
// +build go1.11
|
|
|
|
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
2018-10-13 08:13:05 +02:00
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-10-13 08:10:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2018-10-13 08:13:05 +02:00
|
|
|
desc := tgt.URL.String()
|
2018-10-13 08:10:19 +02:00
|
|
|
if tgt.Name != "" {
|
2018-10-13 08:13:05 +02:00
|
|
|
desc = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
|
2018-10-13 08:10:19 +02:00
|
|
|
}
|
2019-10-07 01:56:21 +02:00
|
|
|
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err)))
|
2018-10-13 08:10:19 +02:00
|
|
|
}
|
|
|
|
proxy.Transport = config.Transport
|
2020-08-28 02:51:27 +02:00
|
|
|
proxy.ModifyResponse = config.ModifyResponse
|
2018-10-13 08:10:19 +02:00
|
|
|
return proxy
|
|
|
|
}
|