Skip to content

Commit 4561d4d

Browse files
committed
feature: add middleware for limiting request to all endpoints
1 parent e4006e9 commit 4561d4d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/users/transport/middlewares.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"net/http"
1111
"strings"
1212

13+
"golang.org/x/time/rate"
14+
1315
"github.com/Edmartt/go-authentication-api/internal/users/models"
1416
"github.com/Edmartt/go-authentication-api/pkg/jwt"
1517
)
@@ -89,3 +91,17 @@ func IsAuthorized(handler http.HandlerFunc) http.HandlerFunc{
8991
handler(w, r.WithContext(ctx))
9092
}
9193
}
94+
95+
96+
97+
func LimitRequest(next http.Handler) http.Handler{
98+
limit := rate.NewLimiter(0.3, 3)
99+
100+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
101+
if limit.Allow() == false{
102+
http.Error(w, http.StatusText(429), http.StatusTooManyRequests)
103+
return
104+
}
105+
next.ServeHTTP(w, r)
106+
})
107+
}

0 commit comments

Comments
 (0)