Commit 0e43271c authored by Matthew Holt's avatar Matthew Holt

Basic proxy feature works

parent 5ae1790e
......@@ -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{}
r.RequestURI = ""
r.URL.Scheme = strings.ToLower(r.URL.Scheme)
var scheme string
if r.TLS == nil {
scheme = "http"
} else {
scheme = "https"
}
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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment