From 858deb6b72ba40c65c1c8dbc705cd1908831ca1c Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Sat, 16 Nov 2024 10:10:35 +0100 Subject: [PATCH] refactoring of code --- internal/proxy.go | 18 ++++++++++++------ internal/route.go | 4 ++-- util/helpers.go | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/internal/proxy.go b/internal/proxy.go index f21f68b..217a6a4 100644 --- a/internal/proxy.go +++ b/internal/proxy.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/jkaninda/goma-gateway/internal/middlewares" "github.com/jkaninda/goma-gateway/pkg/logger" + "github.com/jkaninda/goma-gateway/util" "net/http" "net/http/httputil" "net/url" @@ -80,12 +81,7 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc { // Create proxy proxy := httputil.NewSingleHostReverseProxy(backendURL) // Rewrite - if proxyRoute.path != "" && proxyRoute.rewrite != "" { - // Rewrite the path - if strings.HasPrefix(r.URL.Path, fmt.Sprintf("%s/", proxyRoute.path)) { - r.URL.Path = strings.Replace(r.URL.Path, fmt.Sprintf("%s/", proxyRoute.path), proxyRoute.rewrite, 1) - } - } + rewritePath(r, proxyRoute) // Custom transport with InsecureSkipVerify proxy.Transport = &http.Transport{TLSClientConfig: &tls.Config{ InsecureSkipVerify: proxyRoute.insecureSkipVerify, @@ -105,3 +101,13 @@ func getNextBackend(backendURLs []string) *url.URL { backendURL, _ := url.Parse(backendURLs[idx]) return backendURL } + +// rewritePath rewrites the path if it matches the prefix +func rewritePath(r *http.Request, proxyRoute ProxyRoute) { + if proxyRoute.path != "" && proxyRoute.rewrite != "" { + // Rewrite the path if it matches the prefix + if strings.HasPrefix(r.URL.Path, fmt.Sprintf("%s/", proxyRoute.path)) { + r.URL.Path = util.ParseURLPath(strings.Replace(r.URL.Path, fmt.Sprintf("%s/", proxyRoute.path), proxyRoute.rewrite, 1)) + } + } +} diff --git a/internal/route.go b/internal/route.go index 6ef0b3e..e9e22ec 100644 --- a/internal/route.go +++ b/internal/route.go @@ -64,7 +64,7 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router { logger.Info("Block common exploits enabled") r.Use(middlewares.BlockExploitsMiddleware) } - if gateway.RateLimit > 0 { + if gateway.RateLimit != 0 { // Add rate limit middlewares to all routes, if defined rateLimit := middlewares.RateLimit{ Id: "global_rate", //Generate a unique ID for routes @@ -242,7 +242,7 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router { id = util.Slug(route.Name) } // Apply route rate limit - if route.RateLimit > 0 { + if route.RateLimit != 0 { rateLimit := middlewares.RateLimit{ Id: id, // Use route index as ID Requests: route.RateLimit, diff --git a/util/helpers.go b/util/helpers.go index f3aaf51..c04a42e 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -87,7 +87,7 @@ func MergeSlices(slice1, slice2 []string) []string { return append(slice1, slice2...) } -// ParseURLPath returns a URL path +// ParseURLPath removes duplicated [//] func ParseURLPath(urlPath string) string { // Replace any double slashes with a single slash urlPath = strings.ReplaceAll(urlPath, "//", "/")