From 96229c9e11365c7b8fd6f8515d38a06e4b447ae5 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Sun, 17 Jul 2022 13:47:49 -0400 Subject: [PATCH] counter: change http rate limiter criteria --- plugins/counter/api.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/counter/api.go b/plugins/counter/api.go index ef75bd3..9d0ba18 100644 --- a/plugins/counter/api.go +++ b/plugins/counter/api.go @@ -4,6 +4,7 @@ import ( "embed" "encoding/json" "fmt" + "github.com/velour/catbase/bot/user" "io/ioutil" "net/http" "strconv" @@ -23,11 +24,14 @@ func (p *CounterPlugin) registerWeb() { r := chi.NewRouter() requests := p.cfg.GetInt("counter.requestsPer", 1) seconds := p.cfg.GetInt("counter.seconds", 1) - r.Use(httprate.LimitByIP(requests, time.Duration(seconds)*time.Second)) - r.HandleFunc("/api/users/{user}/items/{item}/increment/{delta}", p.mkIncrementByNAPI(1)) - r.HandleFunc("/api/users/{user}/items/{item}/decrement/{delta}", p.mkIncrementByNAPI(-1)) - r.HandleFunc("/api/users/{user}/items/{item}/increment", p.mkIncrementAPI(1)) - r.HandleFunc("/api/users/{user}/items/{item}/decrement", p.mkIncrementAPI(-1)) + dur := time.Duration(seconds) * time.Second + subrouter := chi.NewRouter() + subrouter.Use(httprate.LimitByIP(requests, dur)) + subrouter.HandleFunc("/api/users/{user}/items/{item}/increment/{delta}", p.mkIncrementByNAPI(1)) + subrouter.HandleFunc("/api/users/{user}/items/{item}/decrement/{delta}", p.mkIncrementByNAPI(-1)) + subrouter.HandleFunc("/api/users/{user}/items/{item}/increment", p.mkIncrementAPI(1)) + subrouter.HandleFunc("/api/users/{user}/items/{item}/decrement", p.mkIncrementAPI(-1)) + r.Mount("/", subrouter) r.HandleFunc("/api", p.handleCounterAPI) r.HandleFunc("/", p.handleCounter) p.b.RegisterWebName(r, "/counter", "Counter") @@ -250,12 +254,14 @@ func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request) if err != nil { w.WriteHeader(500) fmt.Fprint(w, err) + log.Error().Err(err).Msg("Error getting items") return } data, err := json.Marshal(all) if err != nil { w.WriteHeader(500) fmt.Fprint(w, err) + log.Error().Err(err).Msg("Error marshaling items") return } fmt.Fprint(w, string(data))