v1/account API response, rate limiting bla

pull/584/head
binwiederhier 2022-12-17 23:54:19 -05:00
parent ac56fa36ba
commit 56ab34a57f
2 changed files with 34 additions and 4 deletions

View File

@ -40,9 +40,7 @@ func (s *Server) handleAccountGet(w http.ResponseWriter, r *http.Request, v *vis
return err return err
} }
response := &apiAccountSettingsResponse{ response := &apiAccountSettingsResponse{
Usage: &apiAccountUsageLimits{ Usage: &apiAccountUsageLimits{},
Basis: "ip",
},
} }
if v.user != nil { if v.user != nil {
response.Username = v.user.Name response.Username = v.user.Name
@ -66,11 +64,37 @@ func (s *Server) handleAccountGet(w http.ResponseWriter, r *http.Request, v *vis
EmailsLimit: v.user.Plan.EmailsLimit, EmailsLimit: v.user.Plan.EmailsLimit,
AttachmentsBytesLimit: v.user.Plan.AttachmentBytesLimit, AttachmentsBytesLimit: v.user.Plan.AttachmentBytesLimit,
} }
} else {
if v.user.Role == auth.RoleAdmin {
response.Usage.Basis = "account"
response.Plan = &apiAccountSettingsPlan{
Name: "Unlimited",
MessagesLimit: 0,
EmailsLimit: 0,
AttachmentsBytesLimit: 0,
}
} else {
response.Usage.Basis = "ip"
response.Plan = &apiAccountSettingsPlan{
Name: "Free",
MessagesLimit: s.config.VisitorRequestLimitBurst,
EmailsLimit: s.config.VisitorEmailLimitBurst,
AttachmentsBytesLimit: s.config.VisitorAttachmentTotalSizeLimit,
}
}
} }
} else { } else {
response.Username = auth.Everyone response.Username = auth.Everyone
response.Role = string(auth.RoleAnonymous) response.Role = string(auth.RoleAnonymous)
response.Usage.Basis = "account"
response.Plan = &apiAccountSettingsPlan{
Name: "Anonymous",
MessagesLimit: s.config.VisitorRequestLimitBurst,
EmailsLimit: s.config.VisitorEmailLimitBurst,
AttachmentsBytesLimit: s.config.VisitorAttachmentTotalSizeLimit,
}
} }
response.Usage.Messages = int(v.requests.Tokens())
response.Usage.AttachmentsBytes = stats.VisitorAttachmentBytesUsed response.Usage.AttachmentsBytes = stats.VisitorAttachmentBytesUsed
if err := json.NewEncoder(w).Encode(response); err != nil { if err := json.NewEncoder(w).Encode(response); err != nil {
return err return err

View File

@ -45,12 +45,18 @@ type visitorStats struct {
} }
func newVisitor(conf *Config, messageCache *messageCache, ip netip.Addr, user *auth.User) *visitor { func newVisitor(conf *Config, messageCache *messageCache, ip netip.Addr, user *auth.User) *visitor {
var requests *rate.Limiter
if user != nil && user.Plan != nil {
requests = rate.NewLimiter(rate.Limit(user.Plan.MessagesLimit)*rate.Every(24*time.Hour), user.Plan.MessagesLimit)
} else {
requests = rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst)
}
return &visitor{ return &visitor{
config: conf, config: conf,
messageCache: messageCache, messageCache: messageCache,
ip: ip, ip: ip,
user: user, user: user,
requests: rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst), requests: requests,
emails: rate.NewLimiter(rate.Every(conf.VisitorEmailLimitReplenish), conf.VisitorEmailLimitBurst), emails: rate.NewLimiter(rate.Every(conf.VisitorEmailLimitReplenish), conf.VisitorEmailLimitBurst),
subscriptions: util.NewFixedLimiter(int64(conf.VisitorSubscriptionLimit)), subscriptions: util.NewFixedLimiter(int64(conf.VisitorSubscriptionLimit)),
bandwidth: util.NewBytesLimiter(conf.VisitorAttachmentDailyBandwidthLimit, 24*time.Hour), bandwidth: util.NewBytesLimiter(conf.VisitorAttachmentDailyBandwidthLimit, 24*time.Hour),