-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
83 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package middleware | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
"piccolo/api/security" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func RateLimiter() echo.MiddlewareFunc { | ||
limiter := security.NewRedisRateLimiter() | ||
|
||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
ip := c.RealIP() | ||
|
||
res, err := limiter.Limit(ctx, ip) | ||
if err != nil { | ||
slog.Debug(err.Error()) | ||
} | ||
|
||
if res.Allowed <= 0 { | ||
return echo.NewHTTPError(http.StatusTooManyRequests) | ||
} | ||
|
||
return next(c) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package security | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
redisrate "github.com/go-redis/redis_rate/v10" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type RedisRateLimiter struct { | ||
*redisrate.Limiter | ||
} | ||
|
||
const rateRequest = "rate_request_%s" | ||
|
||
func NewRedisRateLimiter() *RedisRateLimiter { | ||
opts, err := redis.ParseURL(os.Getenv("REDIS_URL")) | ||
if err != nil { | ||
log.Fatalf("cannot create redis connection: %v", err) | ||
} | ||
|
||
rdb := redis.NewClient(opts) | ||
|
||
return &RedisRateLimiter{ | ||
redisrate.NewLimiter(rdb), | ||
} | ||
} | ||
|
||
func (rl *RedisRateLimiter) Limit(ctx context.Context, ip string) (*redisrate.Result, error) { | ||
if ip == "" { | ||
return nil, errors.New("IP is not provided") | ||
} | ||
|
||
return rl.Allow(ctx, fmt.Sprintf(rateRequest, ip), redisrate.PerSecond(5)) | ||
} |
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
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
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