refactor: refactoring of rate limiting
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/jkaninda/goma-gateway/util"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// init initializes prometheus metrics
|
||||
@@ -127,6 +128,31 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
||||
}
|
||||
// Apply middlewares to the route
|
||||
for _, middleware := range route.Middlewares {
|
||||
// Apply common exploits to the route
|
||||
// Enable common exploits
|
||||
if route.BlockCommonExploits {
|
||||
logger.Info("Block common exploits enabled")
|
||||
router.Use(middlewares.BlockExploitsMiddleware)
|
||||
}
|
||||
id := string(rune(rIndex))
|
||||
if len(route.Name) != 0 {
|
||||
// Use route name as ID
|
||||
id = util.Slug(route.Name)
|
||||
}
|
||||
// Apply route rate limit
|
||||
if route.RateLimit != 0 {
|
||||
rateLimit := middlewares.RateLimit{
|
||||
Unit: "minute",
|
||||
Id: id, // Use route index as ID
|
||||
Requests: route.RateLimit,
|
||||
Origins: route.Cors.Origins,
|
||||
Hosts: route.Hosts,
|
||||
RedisBased: redisBased,
|
||||
}
|
||||
limiter := rateLimit.NewRateLimiterWindow()
|
||||
// Add rate limit middlewares
|
||||
router.Use(limiter.RateLimitMiddleware())
|
||||
}
|
||||
if len(middleware) != 0 {
|
||||
// Get Access middlewares if it does exist
|
||||
accessMiddleware, err := getMiddleware([]string{middleware}, m)
|
||||
@@ -143,6 +169,31 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
||||
|
||||
}
|
||||
|
||||
// Apply Rate limit middleware
|
||||
if slices.Contains(RateLimitMiddleware, accessMiddleware.Type) {
|
||||
rateLimitMid, err := rateLimitMiddleware(accessMiddleware.Rule)
|
||||
if err != nil {
|
||||
logger.Error("Error: %v", err.Error())
|
||||
}
|
||||
if rateLimitMid.RequestsPerUnit != 0 && route.RateLimit == 0 {
|
||||
rateLimit := middlewares.RateLimit{
|
||||
Unit: rateLimitMid.Unit,
|
||||
Id: id, // Use route index as ID
|
||||
Requests: rateLimitMid.RequestsPerUnit,
|
||||
Origins: route.Cors.Origins,
|
||||
Hosts: route.Hosts,
|
||||
RedisBased: redisBased,
|
||||
PathBased: true,
|
||||
Paths: util.AddPrefixPath(route.Path, accessMiddleware.Paths),
|
||||
}
|
||||
limiter := rateLimit.NewRateLimiterWindow()
|
||||
// Add rate limit middlewares
|
||||
router.Use(limiter.RateLimitMiddleware())
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
// Get route authentication middlewares if it does exist
|
||||
routeMiddleware, err := getMiddleware([]string{middleware}, m)
|
||||
@@ -158,31 +209,6 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
||||
}
|
||||
}
|
||||
|
||||
// Apply common exploits to the route
|
||||
// Enable common exploits
|
||||
if route.BlockCommonExploits {
|
||||
logger.Info("Block common exploits enabled")
|
||||
router.Use(middlewares.BlockExploitsMiddleware)
|
||||
}
|
||||
id := string(rune(rIndex))
|
||||
if len(route.Name) != 0 {
|
||||
// Use route name as ID
|
||||
id = util.Slug(route.Name)
|
||||
}
|
||||
// Apply route rate limit
|
||||
if route.RateLimit != 0 {
|
||||
rateLimit := middlewares.RateLimit{
|
||||
Unit: "minute",
|
||||
Id: id, // Use route index as ID
|
||||
Requests: route.RateLimit,
|
||||
Origins: route.Cors.Origins,
|
||||
Hosts: route.Hosts,
|
||||
RedisBased: redisBased,
|
||||
}
|
||||
limiter := rateLimit.NewRateLimiterWindow()
|
||||
// Add rate limit middlewares
|
||||
router.Use(limiter.RateLimitMiddleware())
|
||||
}
|
||||
// Apply route Cors
|
||||
router.Use(CORSHandler(route.Cors))
|
||||
if len(route.Hosts) > 0 {
|
||||
@@ -208,8 +234,7 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
||||
}
|
||||
router.Use(interceptErrors.ErrorInterceptor)
|
||||
}
|
||||
//r.PathPrefix("/").Handler(proxyRoute.ProxyHandler()) // Proxy handler
|
||||
//r.PathPrefix("").Handler(proxyRoute.ProxyHandler()) // Proxy handler
|
||||
|
||||
} else {
|
||||
logger.Error("Error, path is empty in route %s", route.Name)
|
||||
logger.Error("Route path ignored: %s", route.Path)
|
||||
|
||||
Reference in New Issue
Block a user