-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor reverse proxy setup and cache handling
Simplified reverse proxy configuration and cache request handling by streamlining redundant code. Eliminated unnecessary request cloning for client modification, leading to cleaner and more efficient code maintenance. Issue: #123
- Loading branch information
1 parent
6959974
commit 7c1f33c
Showing
2 changed files
with
13 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,19 @@ | ||
package proxy | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
|
||
"github.com/supporttools/go-web-cache/pkg/cache" | ||
"github.com/supporttools/go-web-cache/pkg/config" | ||
) | ||
|
||
// NewReverseProxy creates and configures a reverse proxy. | ||
func NewReverseProxy(targetURL string, cacheManager cache.CacheManager) *httputil.ReverseProxy { | ||
parsedURL, err := url.Parse(targetURL) | ||
if err != nil { | ||
log.Fatalf("Error parsing target URL '%s': %v", targetURL, err) | ||
} | ||
url, _ := url.Parse(targetURL) | ||
|
||
log.Printf("Creating reverse proxy to %s", parsedURL.String()) | ||
|
||
proxy := httputil.NewSingleHostReverseProxy(parsedURL) | ||
proxy.Transport = &Transport{ | ||
RoundTripper: http.DefaultTransport, | ||
CacheManager: cacheManager, | ||
} | ||
|
||
// Modify proxy.Director for additional debugging and request customization | ||
originalDirector := proxy.Director | ||
proxy.Director = func(req *http.Request) { | ||
log.Printf("Proxying request for %s", req.URL.String()) | ||
|
||
// Call the original director to preserve default behavior | ||
originalDirector(req) | ||
|
||
// Log the request URL to debug | ||
if config.CFG.Debug { | ||
log.Printf("Modified request URL: %s", req.URL.String()) | ||
} | ||
|
||
// Ensure the request host is correctly set | ||
req.URL.Scheme = "http" // or "https", as appropriate | ||
req.URL.Host = parsedURL.Host | ||
req.Host = parsedURL.Host | ||
|
||
// Additional logging to confirm the host and scheme are correctly set | ||
if config.CFG.Debug { | ||
log.Printf("Request URL after modification: %s", req.URL.String()) | ||
log.Printf("Request Host after modification: %s", req.Host) | ||
// Debugging: Log the request headers after modification by the director | ||
log.Printf("Request headers after director modifications:") | ||
for name, values := range req.Header { | ||
for _, value := range values { | ||
log.Printf("%s: %s", name, value) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
// Optionally, customize the Transport to log errors or responses | ||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) { | ||
log.Printf("Error handling request for %s: %v", r.URL.String(), err) | ||
} | ||
proxy := httputil.NewSingleHostReverseProxy(url) | ||
proxy.Transport = &Transport{http.DefaultTransport, cacheManager} | ||
|
||
return proxy | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters