Skip to content

Commit b7ee391

Browse files
authored
Merge pull request #9 from Edmartt/dev
Dev
2 parents 4a6ef2c + 61b397f commit b7ee391

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
172172
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
173173
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
174174
golang.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=
175177
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
176178
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
177179
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=

internal/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (server *HttpServer) SetServer(){
2424

2525
func (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

3030
func (server *HttpServer) SetRoutes(){

internal/users/transport/middlewares.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)