From 13cf98087930f757ffbe9e4928ee5d33e5591109 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 29 Jan 2015 22:05:36 -0700 Subject: [PATCH] Moved proxy middleware into its own package --- middleware/{ => proxy}/proxy.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) rename middleware/{ => proxy}/proxy.go (66%) diff --git a/middleware/proxy.go b/middleware/proxy/proxy.go similarity index 66% rename from middleware/proxy.go rename to middleware/proxy/proxy.go index 5a99f8f0..440b078d 100644 --- a/middleware/proxy.go +++ b/middleware/proxy/proxy.go @@ -1,20 +1,23 @@ -package middleware +// Proxy is middleware that proxies requests. +package proxy import ( "log" "net/http" "strings" + + "github.com/mholt/caddy/middleware" ) -// Proxy is middleware that proxies requests. -func Proxy(p parser) Middleware { +// New creates a new instance of proxy middleware. +func New(c middleware.Controller) (middleware.Middleware, error) { var rules []proxyRule - for p.Next() { + for c.Next() { rule := proxyRule{} - if !p.Args(&rule.from, &rule.to) { - return p.ArgErr() + if !c.Args(&rule.from, &rule.to) { + return nil, c.ArgErr() } rules = append(rules, rule) @@ -24,7 +27,7 @@ func Proxy(p parser) Middleware { return func(w http.ResponseWriter, r *http.Request) { for _, rule := range rules { - if Path(r.URL.Path).Matches(rule.from) { + if middleware.Path(r.URL.Path).Matches(rule.from) { client := &http.Client{} r.RequestURI = "" @@ -41,7 +44,7 @@ func Proxy(p parser) Middleware { } } } - } + }, nil } type proxyRule struct {