0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Basic proxy feature works

This commit is contained in:
Matthew Holt 2015-02-02 23:41:35 -07:00
parent 5ae1790e52
commit 0e43271cc9

View file

@ -4,7 +4,8 @@ package proxy
import (
"log"
"net/http"
"strings"
"net/http/httputil"
"net/url"
"github.com/mholt/caddy/middleware"
)
@ -28,16 +29,22 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
for _, rule := range rules {
if middleware.Path(r.URL.Path).Matches(rule.from) {
client := &http.Client{}
var scheme string
if r.TLS == nil {
scheme = "http"
} else {
scheme = "https"
}
r.RequestURI = ""
r.URL.Scheme = strings.ToLower(r.URL.Scheme)
resp, err := client.Do(r)
baseUrl, err := url.Parse(scheme + "://" + rule.to)
if err != nil {
log.Fatal(err)
}
resp.Write(w)
r.Host = baseUrl.Host
// TODO: Construct this before; not during every request, if possible
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
proxy.ServeHTTP(w, r)
} else {
next(w, r)