mirror of
https://github.com/labstack/echo.git
synced 2024-12-18 16:20:53 +02:00
bc37a3a792
Proxy will be more verbose on errors + possibility to configure custom transport (example: for custom TLS certificates)
25 lines
629 B
Go
25 lines
629 B
Go
// +build go1.11
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/labstack/echo"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
)
|
|
|
|
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) {
|
|
descr := tgt.URL.String()
|
|
if tgt.Name != "" {
|
|
descr = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
|
|
}
|
|
c.Logger().Errorf("remote %s unreachable, could not forward: %v", descr, err)
|
|
c.Error(echo.ErrServiceUnavailable)
|
|
}
|
|
proxy.Transport = config.Transport
|
|
return proxy
|
|
}
|