Files
isp-gate-service/middleware/daily_limit.go
T
txix 36699fdc72
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
2026-08-28 15:17:49: merge [main:657d4302881986193395aa831b36df6fd0798d4d];
2026-08-28 15:17:49 +03:00

44 lines
1.0 KiB
Go

package middleware
import (
"context"
"net/http"
"github.com/txix-open/isp-kit/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 {
if ctx.SkipAppAuth() {
return next.Handle(ctx)
}
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)
})
}
}