Skip to content

Commit

Permalink
Merge pull request #24 from dadav/fix_proxy_issue
Browse files Browse the repository at this point in the history
fix: Try to fix proxy issue
  • Loading branch information
dadav authored Aug 25, 2024
2 parents fca624b + 54aee13 commit 1560926
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/middleware/proxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"bytes"
"net/http"
"net/http/httputil"
"net/url"
Expand All @@ -11,22 +12,28 @@ import (
// capturedResponseWriter is a custom response writer that captures the response status
type capturedResponseWriter struct {
http.ResponseWriter
body []byte
body *bytes.Buffer
status int
}

func NewCapturedResponseWriter(w http.ResponseWriter) *capturedResponseWriter {
return &capturedResponseWriter{
ResponseWriter: w,
body: new(bytes.Buffer),
}
}

func (w *capturedResponseWriter) WriteHeader(code int) {
w.status = code
}

func (w *capturedResponseWriter) Write(body []byte) (int, error) {
w.body = body
return len(body), nil
return w.body.Write(body)
}

func (w *capturedResponseWriter) sendCapturedResponse() {
w.ResponseWriter.WriteHeader(w.status)
w.ResponseWriter.Write(w.body)
w.ResponseWriter.Write(w.body.Bytes())
}

func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
Expand All @@ -42,7 +49,7 @@ func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
func ProxyFallback(upstreamHost string, forwardToProxy func(int) bool, proxiedResponseCb func(*http.Response)) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedResponseWriter := &capturedResponseWriter{ResponseWriter: w}
capturedResponseWriter := NewCapturedResponseWriter(w)
next.ServeHTTP(capturedResponseWriter, r)

if forwardToProxy(capturedResponseWriter.status) {
Expand Down

0 comments on commit 1560926

Please sign in to comment.