Stubbed out really basic proxy middleware

This commit is contained in:
Matthew Holt 2015-01-29 17:17:59 -07:00
parent ec5f94adc8
commit dca59d0eda
2 changed files with 51 additions and 0 deletions

View file

@ -17,6 +17,7 @@ func init() {
register("rewrite", Rewrite)
register("redir", Redirect)
register("ext", Extensionless)
register("proxy", Proxy)
register("fastcgi", FastCGI)
}

50
middleware/proxy.go Normal file
View file

@ -0,0 +1,50 @@
package middleware
import (
"log"
"net/http"
"strings"
)
// Proxy is middleware that proxies requests.
func Proxy(p parser) Middleware {
var rules []proxyRule
for p.Next() {
rule := proxyRule{}
if !p.Args(&rule.from, &rule.to) {
return p.ArgErr()
}
rules = append(rules, rule)
}
return func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
for _, rule := range rules {
if Path(r.URL.Path).Matches(rule.from) {
client := &http.Client{}
r.RequestURI = ""
r.URL.Scheme = strings.ToLower(r.URL.Scheme)
resp, err := client.Do(r)
if err != nil {
log.Fatal(err)
}
resp.Write(w)
} else {
next(w, r)
}
}
}
}
}
type proxyRule struct {
from string
to string
}