mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-22 17:49:59 +08:00
Moved proxy middleware into its own package
This commit is contained in:
53
middleware/proxy/proxy.go
Normal file
53
middleware/proxy/proxy.go
Normal file
@ -0,0 +1,53 @@
|
||||
// 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
|
||||
}
|
Reference in New Issue
Block a user