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

Basic proxy feature works

parent 5ae1790e
...@@ -4,7 +4,8 @@ package proxy ...@@ -4,7 +4,8 @@ package proxy
import ( import (
"log" "log"
"net/http" "net/http"
"strings" "net/http/httputil"
"net/url"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
) )
...@@ -28,16 +29,22 @@ func New(c middleware.Controller) (middleware.Middleware, error) { ...@@ -28,16 +29,22 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
for _, rule := range rules { for _, rule := range rules {
if middleware.Path(r.URL.Path).Matches(rule.from) { if middleware.Path(r.URL.Path).Matches(rule.from) {
client := &http.Client{} var scheme string
if r.TLS == nil {
r.RequestURI = "" scheme = "http"
r.URL.Scheme = strings.ToLower(r.URL.Scheme) } else {
scheme = "https"
}
resp, err := client.Do(r) baseUrl, err := url.Parse(scheme + "://" + rule.to)
if err != nil { if err != nil {
log.Fatal(err) 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 { } else {
next(w, r) 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