Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReverseProxy workaround #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,28 @@ func ginRecovery(c *gin.Context) {
c.Next()
}

// roundTripper makes func signature a http.RoundTripper
type roundTripper func(*http.Request) (*http.Response, error)
func (f roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) }

type proxyRefactor url.URL
func (target *proxyRefactor) rt(req *http.Request) (*http.Response, error) {
req.Host = target.Host
return http.DefaultTransport.RoundTrip(req)
}

func (target *proxyRefactor) direct(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.Host = target.Host
}

func ReverseProxy(target *url.URL) gin.HandlerFunc {
proxy := httputil.NewSingleHostReverseProxy((*nativeurl.URL)(target))
refactor := proxyRefactor(*target)
proxy.Transport = roundTripper(refactor.rt)
proxy.Director = refactor.direct

return func(c *gin.Context) {
proxy.ServeHTTP(c.Writer, c.Request)
}
Expand Down Expand Up @@ -383,6 +403,9 @@ func reverseProxyForService(service string) *httputil.ReverseProxy {
reverseProxiesMapMutex.Lock()
defer reverseProxiesMapMutex.Unlock()
rp := httputil.NewSingleHostReverseProxy(u)
refactor := proxyRefactor(*u)
rp.Transport = roundTripper(refactor.rt)
rp.Director = refactor.direct

buf := new(bytes.Buffer)
rp.ErrorLog = stdlog.New(buf, "reverseProxy ", stdlog.LUTC)
Expand Down