forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proxy: Better errors + remote custom TLS (labstack#1197)
Proxy will be more verbose on errors + possibility to configure custom transport (example: for custom TLS certificates)
- Loading branch information
Showing
6 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// +build !go1.11 | ||
|
||
package middleware | ||
|
||
import ( | ||
"github.com/labstack/echo" | ||
"net/http" | ||
"net/http/httputil" | ||
) | ||
|
||
func proxyHTTP(t *ProxyTarget, c echo.Context, config ProxyConfig) http.Handler { | ||
return httputil.NewSingleHostReverseProxy(t.URL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// +build go1.11 | ||
|
||
package middleware | ||
|
||
import ( | ||
"github.com/labstack/echo" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestProxy_1_11(t *testing.T) { | ||
// Setup | ||
url1, _ := url.Parse("http://127.0.0.1:27121") | ||
url2, _ := url.Parse("http://127.0.0.1:27122") | ||
|
||
targets := []*ProxyTarget{ | ||
{ | ||
Name: "target 1", | ||
URL: url1, | ||
}, | ||
{ | ||
Name: "target 2", | ||
URL: url2, | ||
}, | ||
} | ||
rb := NewRandomBalancer(nil) | ||
// must add targets: | ||
for _, target := range targets { | ||
assert.True(t, rb.AddTarget(target)) | ||
} | ||
|
||
// must ignore duplicates: | ||
for _, target := range targets { | ||
assert.False(t, rb.AddTarget(target)) | ||
} | ||
|
||
// Random | ||
e := echo.New() | ||
e.Use(Proxy(rb)) | ||
req := httptest.NewRequest(echo.GET, "/", nil) | ||
rec := newCloseNotifyRecorder() | ||
|
||
// Remote unreachable | ||
rec = newCloseNotifyRecorder() | ||
req.URL.Path = "/api/users" | ||
e.ServeHTTP(rec, req) | ||
assert.Equal(t, "/api/users", req.URL.Path) | ||
assert.Equal(t, http.StatusServiceUnavailable, rec.Code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters