Commit dca59d0e authored by Matthew Holt's avatar Matthew Holt

Stubbed out really basic proxy middleware

parent ec5f94ad
......@@ -17,6 +17,7 @@ func init() {
register("rewrite", Rewrite)
register("redir", Redirect)
register("ext", Extensionless)
register("proxy", Proxy)
register("fastcgi", FastCGI)
}
......
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
}
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