Commit d8e7adcd authored by Matthew Holt's avatar Matthew Holt

Refactored proxy middleware

parent 113b175d
......@@ -2,7 +2,6 @@
package proxy
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
......@@ -11,60 +10,75 @@ import (
"github.com/mholt/caddy/middleware"
)
// New creates a new instance of proxy middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
var rules []proxyRule
// Proxy represents a middleware instance that can proxy requests.
type Proxy struct {
Next middleware.Handler
Rules []Rule
}
for c.Next() {
rule := proxyRule{}
// ServeHTTP satisfies the middleware.Handler interface.
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if !c.Args(&rule.from, &rule.to) {
return nil, c.ArgErr()
}
for _, rule := range p.Rules {
if middleware.Path(r.URL.Path).Matches(rule.From) {
var base string
rules = append(rules, rule)
if strings.HasPrefix(rule.To, "http") { // includes https
// destination includes a scheme! no need to guess
base = rule.To
} else {
// no scheme specified; assume same as request
var scheme string
if r.TLS == nil {
scheme = "http"
} else {
scheme = "https"
}
base = scheme + "://" + rule.To
}
baseUrl, err := url.Parse(base)
if err != nil {
return http.StatusInternalServerError, err
}
r.Host = baseUrl.Host
// TODO: Construct this before; not during every request, if possible
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
proxy.ServeHTTP(w, r)
return 0, nil
}
}
return func(next middleware.Handler) middleware.Handler {
return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return p.Next.ServeHTTP(w, r)
}
for _, rule := range rules {
if middleware.Path(r.URL.Path).Matches(rule.from) {
var base string
// New creates a new instance of proxy middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
rules, err := parse(c)
if err != nil {
return nil, err
}
if strings.HasPrefix(rule.to, "http") { // includes https
// destination includes a scheme! no need to guess
base = rule.to
} else {
// no scheme specified; assume same as request
var scheme string
if r.TLS == nil {
scheme = "http"
} else {
scheme = "https"
}
base = scheme + "://" + rule.to
}
return func(next middleware.Handler) middleware.Handler {
return Proxy{Next: next, Rules: rules}
}, nil
}
baseUrl, err := url.Parse(base)
if err != nil {
log.Fatal(err)
}
r.Host = baseUrl.Host
func parse(c middleware.Controller) ([]Rule, error) {
var rules []Rule
// TODO: Construct this before; not during every request, if possible
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
proxy.ServeHTTP(w, r)
return 0, nil
}
}
for c.Next() {
var rule Rule
if !c.Args(&rule.From, &rule.To) {
return rules, c.ArgErr()
}
rules = append(rules, rule)
}
return next.ServeHTTP(w, r)
})
}, nil
return rules, nil
}
type proxyRule struct {
from string
to string
type Rule struct {
From, To string
}
......@@ -108,7 +108,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
Path: path,
Command: cmd,
Arguments: args,
Respawn: respawn,
Respawn: respawn, // TODO: This isn't used currently
})
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment