17 lines
353 B
Go
17 lines
353 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
func limit(limiter *rate.Limiter, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !limiter.Allow() {
|
|
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|