feat(api-key): add independent quota and expiration support
This feature allows API Keys to have their own quota limits and expiration times, independent of the user's balance. Backend: - Add quota, quota_used, expires_at fields to api_key schema - Implement IsExpired() and IsQuotaExhausted() checks in middleware - Add ResetQuota and ClearExpiration API endpoints - Integrate quota billing in gateway handlers (OpenAI, Anthropic, Gemini) - Include quota/expiration fields in auth cache for performance - Expiration check returns 403, quota exhausted returns 429 Frontend: - Add quota and expiration inputs to key create/edit dialog - Add quick-select buttons for expiration (+7, +30, +90 days) - Add reset quota confirmation dialog - Add expires_at column to keys list - Add i18n translations for new features (en/zh) Migration: - Add 045_add_api_key_quota.sql for new columns
This commit is contained in:
@@ -170,6 +170,68 @@ func (_u *APIKeyUpdate) ClearIPBlacklist() *APIKeyUpdate {
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetQuota sets the "quota" field.
|
||||
func (_u *APIKeyUpdate) SetQuota(v float64) *APIKeyUpdate {
|
||||
_u.mutation.ResetQuota()
|
||||
_u.mutation.SetQuota(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableQuota sets the "quota" field if the given value is not nil.
|
||||
func (_u *APIKeyUpdate) SetNillableQuota(v *float64) *APIKeyUpdate {
|
||||
if v != nil {
|
||||
_u.SetQuota(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// AddQuota adds value to the "quota" field.
|
||||
func (_u *APIKeyUpdate) AddQuota(v float64) *APIKeyUpdate {
|
||||
_u.mutation.AddQuota(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetQuotaUsed sets the "quota_used" field.
|
||||
func (_u *APIKeyUpdate) SetQuotaUsed(v float64) *APIKeyUpdate {
|
||||
_u.mutation.ResetQuotaUsed()
|
||||
_u.mutation.SetQuotaUsed(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableQuotaUsed sets the "quota_used" field if the given value is not nil.
|
||||
func (_u *APIKeyUpdate) SetNillableQuotaUsed(v *float64) *APIKeyUpdate {
|
||||
if v != nil {
|
||||
_u.SetQuotaUsed(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// AddQuotaUsed adds value to the "quota_used" field.
|
||||
func (_u *APIKeyUpdate) AddQuotaUsed(v float64) *APIKeyUpdate {
|
||||
_u.mutation.AddQuotaUsed(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetExpiresAt sets the "expires_at" field.
|
||||
func (_u *APIKeyUpdate) SetExpiresAt(v time.Time) *APIKeyUpdate {
|
||||
_u.mutation.SetExpiresAt(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
|
||||
func (_u *APIKeyUpdate) SetNillableExpiresAt(v *time.Time) *APIKeyUpdate {
|
||||
if v != nil {
|
||||
_u.SetExpiresAt(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearExpiresAt clears the value of the "expires_at" field.
|
||||
func (_u *APIKeyUpdate) ClearExpiresAt() *APIKeyUpdate {
|
||||
_u.mutation.ClearExpiresAt()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetUser sets the "user" edge to the User entity.
|
||||
func (_u *APIKeyUpdate) SetUser(v *User) *APIKeyUpdate {
|
||||
return _u.SetUserID(v.ID)
|
||||
@@ -350,6 +412,24 @@ func (_u *APIKeyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
||||
if _u.mutation.IPBlacklistCleared() {
|
||||
_spec.ClearField(apikey.FieldIPBlacklist, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Quota(); ok {
|
||||
_spec.SetField(apikey.FieldQuota, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AddedQuota(); ok {
|
||||
_spec.AddField(apikey.FieldQuota, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.QuotaUsed(); ok {
|
||||
_spec.SetField(apikey.FieldQuotaUsed, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AddedQuotaUsed(); ok {
|
||||
_spec.AddField(apikey.FieldQuotaUsed, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.ExpiresAt(); ok {
|
||||
_spec.SetField(apikey.FieldExpiresAt, field.TypeTime, value)
|
||||
}
|
||||
if _u.mutation.ExpiresAtCleared() {
|
||||
_spec.ClearField(apikey.FieldExpiresAt, field.TypeTime)
|
||||
}
|
||||
if _u.mutation.UserCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.M2O,
|
||||
@@ -611,6 +691,68 @@ func (_u *APIKeyUpdateOne) ClearIPBlacklist() *APIKeyUpdateOne {
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetQuota sets the "quota" field.
|
||||
func (_u *APIKeyUpdateOne) SetQuota(v float64) *APIKeyUpdateOne {
|
||||
_u.mutation.ResetQuota()
|
||||
_u.mutation.SetQuota(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableQuota sets the "quota" field if the given value is not nil.
|
||||
func (_u *APIKeyUpdateOne) SetNillableQuota(v *float64) *APIKeyUpdateOne {
|
||||
if v != nil {
|
||||
_u.SetQuota(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// AddQuota adds value to the "quota" field.
|
||||
func (_u *APIKeyUpdateOne) AddQuota(v float64) *APIKeyUpdateOne {
|
||||
_u.mutation.AddQuota(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetQuotaUsed sets the "quota_used" field.
|
||||
func (_u *APIKeyUpdateOne) SetQuotaUsed(v float64) *APIKeyUpdateOne {
|
||||
_u.mutation.ResetQuotaUsed()
|
||||
_u.mutation.SetQuotaUsed(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableQuotaUsed sets the "quota_used" field if the given value is not nil.
|
||||
func (_u *APIKeyUpdateOne) SetNillableQuotaUsed(v *float64) *APIKeyUpdateOne {
|
||||
if v != nil {
|
||||
_u.SetQuotaUsed(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// AddQuotaUsed adds value to the "quota_used" field.
|
||||
func (_u *APIKeyUpdateOne) AddQuotaUsed(v float64) *APIKeyUpdateOne {
|
||||
_u.mutation.AddQuotaUsed(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetExpiresAt sets the "expires_at" field.
|
||||
func (_u *APIKeyUpdateOne) SetExpiresAt(v time.Time) *APIKeyUpdateOne {
|
||||
_u.mutation.SetExpiresAt(v)
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
|
||||
func (_u *APIKeyUpdateOne) SetNillableExpiresAt(v *time.Time) *APIKeyUpdateOne {
|
||||
if v != nil {
|
||||
_u.SetExpiresAt(*v)
|
||||
}
|
||||
return _u
|
||||
}
|
||||
|
||||
// ClearExpiresAt clears the value of the "expires_at" field.
|
||||
func (_u *APIKeyUpdateOne) ClearExpiresAt() *APIKeyUpdateOne {
|
||||
_u.mutation.ClearExpiresAt()
|
||||
return _u
|
||||
}
|
||||
|
||||
// SetUser sets the "user" edge to the User entity.
|
||||
func (_u *APIKeyUpdateOne) SetUser(v *User) *APIKeyUpdateOne {
|
||||
return _u.SetUserID(v.ID)
|
||||
@@ -821,6 +963,24 @@ func (_u *APIKeyUpdateOne) sqlSave(ctx context.Context) (_node *APIKey, err erro
|
||||
if _u.mutation.IPBlacklistCleared() {
|
||||
_spec.ClearField(apikey.FieldIPBlacklist, field.TypeJSON)
|
||||
}
|
||||
if value, ok := _u.mutation.Quota(); ok {
|
||||
_spec.SetField(apikey.FieldQuota, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AddedQuota(); ok {
|
||||
_spec.AddField(apikey.FieldQuota, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.QuotaUsed(); ok {
|
||||
_spec.SetField(apikey.FieldQuotaUsed, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.AddedQuotaUsed(); ok {
|
||||
_spec.AddField(apikey.FieldQuotaUsed, field.TypeFloat64, value)
|
||||
}
|
||||
if value, ok := _u.mutation.ExpiresAt(); ok {
|
||||
_spec.SetField(apikey.FieldExpiresAt, field.TypeTime, value)
|
||||
}
|
||||
if _u.mutation.ExpiresAtCleared() {
|
||||
_spec.ClearField(apikey.FieldExpiresAt, field.TypeTime)
|
||||
}
|
||||
if _u.mutation.UserCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.M2O,
|
||||
|
||||
Reference in New Issue
Block a user