first commit
This commit is contained in:
27
internal/http/middleware/ratelimit.go
Normal file
27
internal/http/middleware/ratelimit.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/httprate"
|
||||
)
|
||||
|
||||
// RateLimit returns a middleware that limits requests per minute by IP.
|
||||
// Admin routes are skipped.
|
||||
func RateLimit(perMin int) func(http.Handler) http.Handler {
|
||||
if perMin <= 0 {
|
||||
return func(next http.Handler) http.Handler { return next }
|
||||
}
|
||||
limiter := httprate.LimitByIP(perMin, time.Minute)
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/v1/admin/") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
limiter(next).ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user