mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-05 08:38:26 +03:00
Stubbed out really basic proxy middleware
This commit is contained in:
parent
ec5f94adc8
commit
dca59d0eda
2 changed files with 51 additions and 0 deletions
|
@ -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
50
middleware/proxy.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue