0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/proxy/proxy.go
2015-01-29 23:52:18 -07:00

53 lines
944 B
Go

// Package proxy is middleware that proxies requests.
package proxy
import (
"log"
"net/http"
"strings"
"github.com/mholt/caddy/middleware"
)
// New creates a new instance of proxy middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
var rules []proxyRule
for c.Next() {
rule := proxyRule{}
if !c.Args(&rule.from, &rule.to) {
return nil, c.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 middleware.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)
}
}
}
}, nil
}
type proxyRule struct {
from string
to string
}