Skip to content

Commit e1e3ebe

Browse files
committed
Proxy middleware refactor
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent e827c85 commit e1e3ebe

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

middleware/body_dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func BodyDump(handler BodyDumpHandler) echo.MiddlewareFunc {
5050
}
5151

5252
// BodyDumpWithConfig returns a BodyDump middleware with config.
53-
// See: `BodyConfig()`.
53+
// See: `BodyDump()`.
5454
func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {
5555
// Defaults
5656
if config.Handler == nil {

middleware/proxy.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ type (
5353
}
5454
)
5555

56+
var (
57+
// DefaultProxyConfig is the default Proxy middleware config.
58+
DefaultProxyConfig = ProxyConfig{
59+
Skipper: DefaultSkipper,
60+
}
61+
)
62+
5663
func proxyHTTP(t *ProxyTarget) http.Handler {
5764
return httputil.NewSingleHostReverseProxy(t.URL)
5865
}
@@ -113,8 +120,18 @@ func (r *RoundRobinBalancer) Next() *ProxyTarget {
113120
return t
114121
}
115122

116-
// Proxy returns an HTTP/WebSocket reverse proxy middleware.
117-
func Proxy(config ProxyConfig) echo.MiddlewareFunc {
123+
// Proxy returns a Proxy middleware.
124+
//
125+
// Proxy middleware forwards a request to upstream server using a configured load balancing technique.
126+
func Proxy(balancer ProxyBalancer) echo.MiddlewareFunc {
127+
c := DefaultProxyConfig
128+
c.Balancer = balancer
129+
return ProxyWithConfig(c)
130+
}
131+
132+
// ProxyWithConfig returns a Proxy middleware with config.
133+
// See: `Proxy()`
134+
func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
118135
// Defaults
119136
if config.Skipper == nil {
120137
config.Skipper = DefaultLoggerConfig.Skipper

middleware/proxy_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ func TestProxy(t *testing.T) {
5555
URL: url2,
5656
},
5757
}
58-
config := ProxyConfig{
59-
Balancer: &RandomBalancer{
60-
Targets: targets,
61-
},
58+
rb := &RandomBalancer{
59+
Targets: targets,
6260
}
6361

6462
// Random
6563
e := echo.New()
66-
e.Use(Proxy(config))
64+
e.Use(Proxy(rb))
6765
req := httptest.NewRequest(echo.GET, "/", nil)
6866
rec := newCloseNotifyRecorder()
6967
e.ServeHTTP(rec, req)
@@ -77,11 +75,11 @@ func TestProxy(t *testing.T) {
7775
})
7876

7977
// Round-robin
80-
config.Balancer = &RoundRobinBalancer{
78+
rrb := &RoundRobinBalancer{
8179
Targets: targets,
8280
}
8381
e = echo.New()
84-
e.Use(Proxy(config))
82+
e.Use(Proxy(rrb))
8583
rec = newCloseNotifyRecorder()
8684
e.ServeHTTP(rec, req)
8785
body = rec.Body.String()

0 commit comments

Comments
 (0)