2015-09-18 01:36:58 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-03-07 16:19:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-09-18 01:36:58 -05:00
|
|
|
)
|
|
|
|
|
2019-02-11 14:12:01 -06:00
|
|
|
// Quota returns a function that returns a function used to call quotaservice based on target name
|
2022-02-10 05:42:06 -06:00
|
|
|
func Quota(quotaService quota.Service) func(string) web.Handler {
|
2021-08-25 08:11:22 -05:00
|
|
|
if quotaService == nil {
|
|
|
|
panic("quotaService is nil")
|
|
|
|
}
|
2019-02-11 14:12:01 -06:00
|
|
|
//https://open.spotify.com/track/7bZSoBEAEEUsGEuLOf94Jm?si=T1Tdju5qRSmmR0zph_6RBw fuuuuunky
|
2021-10-11 07:30:59 -05:00
|
|
|
return func(target string) web.Handler {
|
2020-02-28 05:50:58 -06:00
|
|
|
return func(c *models.ReqContext) {
|
2019-02-11 14:12:01 -06:00
|
|
|
limitReached, err := quotaService.QuotaReached(c, target)
|
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.JsonApiErr(500, "Failed to get quota", err)
|
2019-02-11 14:12:01 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if limitReached {
|
|
|
|
c.JsonApiErr(403, fmt.Sprintf("%s Quota reached", target), nil)
|
|
|
|
return
|
|
|
|
}
|
2015-09-18 01:36:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|