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:
parent
5ae1790e52
commit
0e43271cc9
1 changed files with 14 additions and 7 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue