refactoring of code

This commit is contained in:
Jonas Kaninda
2024-11-16 10:10:35 +01:00
parent 434c48d3ec
commit 858deb6b72
3 changed files with 15 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"github.com/jkaninda/goma-gateway/internal/middlewares" "github.com/jkaninda/goma-gateway/internal/middlewares"
"github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/jkaninda/goma-gateway/util"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
@@ -80,12 +81,7 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc {
// Create proxy // Create proxy
proxy := httputil.NewSingleHostReverseProxy(backendURL) proxy := httputil.NewSingleHostReverseProxy(backendURL)
// Rewrite // Rewrite
if proxyRoute.path != "" && proxyRoute.rewrite != "" { rewritePath(r, proxyRoute)
// 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)
}
}
// Custom transport with InsecureSkipVerify // Custom transport with InsecureSkipVerify
proxy.Transport = &http.Transport{TLSClientConfig: &tls.Config{ proxy.Transport = &http.Transport{TLSClientConfig: &tls.Config{
InsecureSkipVerify: proxyRoute.insecureSkipVerify, InsecureSkipVerify: proxyRoute.insecureSkipVerify,
@@ -105,3 +101,13 @@ func getNextBackend(backendURLs []string) *url.URL {
backendURL, _ := url.Parse(backendURLs[idx]) backendURL, _ := url.Parse(backendURLs[idx])
return backendURL 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))
}
}
}

View File

@@ -64,7 +64,7 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
logger.Info("Block common exploits enabled") logger.Info("Block common exploits enabled")
r.Use(middlewares.BlockExploitsMiddleware) r.Use(middlewares.BlockExploitsMiddleware)
} }
if gateway.RateLimit > 0 { if gateway.RateLimit != 0 {
// Add rate limit middlewares to all routes, if defined // Add rate limit middlewares to all routes, if defined
rateLimit := middlewares.RateLimit{ rateLimit := middlewares.RateLimit{
Id: "global_rate", //Generate a unique ID for routes Id: "global_rate", //Generate a unique ID for routes
@@ -242,7 +242,7 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
id = util.Slug(route.Name) id = util.Slug(route.Name)
} }
// Apply route rate limit // Apply route rate limit
if route.RateLimit > 0 { if route.RateLimit != 0 {
rateLimit := middlewares.RateLimit{ rateLimit := middlewares.RateLimit{
Id: id, // Use route index as ID Id: id, // Use route index as ID
Requests: route.RateLimit, Requests: route.RateLimit,

View File

@@ -87,7 +87,7 @@ func MergeSlices(slice1, slice2 []string) []string {
return append(slice1, slice2...) return append(slice1, slice2...)
} }
// ParseURLPath returns a URL path // ParseURLPath removes duplicated [//]
func ParseURLPath(urlPath string) string { func ParseURLPath(urlPath string) string {
// Replace any double slashes with a single slash // Replace any double slashes with a single slash
urlPath = strings.ReplaceAll(urlPath, "//", "/") urlPath = strings.ReplaceAll(urlPath, "//", "/")