remove rate limiter, job of reverse proxy
This commit is contained in:
parent
a7392da5fb
commit
f8e7746f6c
2 changed files with 1 additions and 52 deletions
9
main.go
9
main.go
|
@ -33,14 +33,7 @@ func run() error {
|
|||
handler := http.Handler(mux)
|
||||
|
||||
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
||||
mux.Handle(
|
||||
"POST /api/password",
|
||||
middlewares.RateLimiter(
|
||||
routes.CreatePassword(store, maxPasswordLength),
|
||||
1*time.Minute,
|
||||
5,
|
||||
),
|
||||
)
|
||||
mux.Handle("POST /api/password", routes.CreatePassword(store, maxPasswordLength))
|
||||
mux.Handle("GET /api/password/{id}", routes.GetPassword(store))
|
||||
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(store))
|
||||
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RateLimiter(handler http.Handler, clearInterval time.Duration, maxRequests int) http.HandlerFunc {
|
||||
requests := make(map[string]int)
|
||||
lock := sync.Mutex{}
|
||||
|
||||
ticker := time.NewTicker(clearInterval)
|
||||
go func() {
|
||||
for {
|
||||
<-ticker.C
|
||||
|
||||
lock.Lock()
|
||||
clear(requests)
|
||||
lock.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
return func(res http.ResponseWriter, req *http.Request) {
|
||||
addr := req.RemoteAddr
|
||||
|
||||
lock.Lock()
|
||||
count, found := requests[addr]
|
||||
if !found {
|
||||
count = 0
|
||||
}
|
||||
|
||||
count += 1
|
||||
requests[addr] = count
|
||||
lock.Unlock()
|
||||
|
||||
if count > maxRequests {
|
||||
http.Error(res, "Too many requests", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
handler.ServeHTTP(res, req)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue