mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-05 18:44:58 +03:00
Moved proxy middleware into its own package
This commit is contained in:
parent
e6063fb26b
commit
13cf980879
1 changed files with 11 additions and 8 deletions
|
@ -1,20 +1,23 @@
|
||||||
package middleware
|
// Proxy is middleware that proxies requests.
|
||||||
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mholt/caddy/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Proxy is middleware that proxies requests.
|
// New creates a new instance of proxy middleware.
|
||||||
func Proxy(p parser) Middleware {
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
var rules []proxyRule
|
var rules []proxyRule
|
||||||
|
|
||||||
for p.Next() {
|
for c.Next() {
|
||||||
rule := proxyRule{}
|
rule := proxyRule{}
|
||||||
|
|
||||||
if !p.Args(&rule.from, &rule.to) {
|
if !c.Args(&rule.from, &rule.to) {
|
||||||
return p.ArgErr()
|
return nil, c.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
rules = append(rules, rule)
|
rules = append(rules, rule)
|
||||||
|
@ -24,7 +27,7 @@ func Proxy(p parser) Middleware {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
if Path(r.URL.Path).Matches(rule.from) {
|
if middleware.Path(r.URL.Path).Matches(rule.from) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
r.RequestURI = ""
|
r.RequestURI = ""
|
||||||
|
@ -41,7 +44,7 @@ func Proxy(p parser) Middleware {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type proxyRule struct {
|
type proxyRule struct {
|
Loading…
Reference in a new issue