File tree Expand file tree Collapse file tree 4 files changed +20
-1
lines changed Expand file tree Collapse file tree 4 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -27,4 +27,5 @@ require (
2727 github.com/mattn/go-sqlite3 v1.14.9 // indirect
2828 golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
2929 golang.org/x/text v0.3.7 // indirect
30+ golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
3031)
Original file line number Diff line number Diff line change @@ -172,6 +172,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
172172golang.org/x/text v0.3.6 /go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ =
173173golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk =
174174golang.org/x/text v0.3.7 /go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ =
175+ golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w =
176+ golang.org/x/time v0.0.0-20220411224347-583f2d630306 /go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ =
175177golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e /go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ =
176178golang.org/x/tools v0.0.0-20190311212946-11955173bddd /go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs =
177179golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc /go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q =
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ func (server *HttpServer) SetServer(){
2424
2525func (server * HttpServer ) StartServer (port string ){
2626 fmt .Println ("Server Started in: " , port )
27- log .Fatal (http .ListenAndServe (port , server .Router ))
27+ log .Fatal (http .ListenAndServe (port , transport . LimitRequest ( server .Router ) ))
2828}
2929
3030func (server * HttpServer ) SetRoutes (){
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments