MSP-437 implement daily limit middleware
This commit is contained in:
1 parent
0367fa8ac7
commit
d71dc62634
1 file changed
+39
@@ -0,0 +1,39 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/httperrors"
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
type DailyLimitChecker interface {
|
||||
IncrementAndCheck(ctx context.Context, applicationId int) (bool, error)
|
||||
}
|
||||
|
||||
func DailyLimit(checker DailyLimitChecker) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
authData, err := ctx.GetAuthData()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "daily limit: get auth data")
|
||||
}
|
||||
|
||||
ok, err := checker.IncrementAndCheck(ctx.Context(), authData.ApplicationId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "daily limit: increment and check")
|
||||
}
|
||||
if !ok {
|
||||
return httperrors.New(
|
||||
http.StatusTooManyRequests,
|
||||
"daily requests limit has been reached",
|
||||
errors.Errorf("daily limit: daily requests limit has been reached for application '%d'", authData.ApplicationId),
|
||||
)
|
||||
}
|
||||
|
||||
return next.Handle(ctx)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user