api_key.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package schema
  2. import (
  3. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  4. "github.com/Wei-Shaw/sub2api/internal/domain"
  5. "entgo.io/ent"
  6. "entgo.io/ent/dialect"
  7. "entgo.io/ent/dialect/entsql"
  8. "entgo.io/ent/schema"
  9. "entgo.io/ent/schema/edge"
  10. "entgo.io/ent/schema/field"
  11. "entgo.io/ent/schema/index"
  12. )
  13. // APIKey holds the schema definition for the APIKey entity.
  14. type APIKey struct {
  15. ent.Schema
  16. }
  17. func (APIKey) Annotations() []schema.Annotation {
  18. return []schema.Annotation{
  19. entsql.Annotation{Table: "api_keys"},
  20. }
  21. }
  22. func (APIKey) Mixin() []ent.Mixin {
  23. return []ent.Mixin{
  24. mixins.TimeMixin{},
  25. mixins.SoftDeleteMixin{},
  26. }
  27. }
  28. func (APIKey) Fields() []ent.Field {
  29. return []ent.Field{
  30. field.Int64("user_id"),
  31. field.String("key").
  32. MaxLen(128).
  33. Optional().
  34. NotEmpty().
  35. Unique(),
  36. // Compatibility metadata for historical hash-only keys; new keys use key.
  37. field.String("key_hash").MaxLen(64).Optional().Sensitive(),
  38. field.String("key_prefix").MaxLen(16).Optional(),
  39. field.String("name").
  40. MaxLen(100).
  41. NotEmpty(),
  42. field.Int64("group_id").
  43. Optional().
  44. Nillable(),
  45. field.String("status").
  46. MaxLen(20).
  47. Default(domain.StatusActive),
  48. field.Time("last_used_at").
  49. Optional().
  50. Nillable().
  51. Comment("Last usage time of this API key"),
  52. field.JSON("ip_whitelist", []string{}).
  53. Optional().
  54. Comment("Allowed IPs/CIDRs, e.g. [\"192.168.1.100\", \"10.0.0.0/8\"]"),
  55. field.JSON("ip_blacklist", []string{}).
  56. Optional().
  57. Comment("Blocked IPs/CIDRs"),
  58. // ========== Quota fields ==========
  59. // Quota limit in USD (0 = unlimited)
  60. field.Float("quota").
  61. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  62. Default(0).
  63. Comment("Quota limit in USD for this API key (0 = unlimited)"),
  64. // Used quota amount
  65. field.Float("quota_used").
  66. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  67. Default(0).
  68. Comment("Used quota amount in USD"),
  69. // Expiration time (nil = never expires)
  70. field.Time("expires_at").
  71. Optional().
  72. Nillable().
  73. Comment("Expiration time for this API key (null = never expires)"),
  74. // ========== Rate limit fields ==========
  75. // Rate limit configuration (0 = unlimited)
  76. field.Float("rate_limit_5h").
  77. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  78. Default(0).
  79. Comment("Rate limit in USD per 5 hours (0 = unlimited)"),
  80. field.Float("rate_limit_1d").
  81. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  82. Default(0).
  83. Comment("Rate limit in USD per day (0 = unlimited)"),
  84. field.Float("rate_limit_7d").
  85. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  86. Default(0).
  87. Comment("Rate limit in USD per 7 days (0 = unlimited)"),
  88. // Rate limit usage tracking
  89. field.Float("usage_5h").
  90. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  91. Default(0).
  92. Comment("Used amount in USD for the current 5h window"),
  93. field.Float("usage_1d").
  94. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  95. Default(0).
  96. Comment("Used amount in USD for the current 1d window"),
  97. field.Float("usage_7d").
  98. SchemaType(map[string]string{dialect.Postgres: "decimal(20,8)"}).
  99. Default(0).
  100. Comment("Used amount in USD for the current 7d window"),
  101. // Window start times
  102. field.Time("window_5h_start").
  103. Optional().
  104. Nillable().
  105. Comment("Start time of the current 5h rate limit window"),
  106. field.Time("window_1d_start").
  107. Optional().
  108. Nillable().
  109. Comment("Start time of the current 1d rate limit window"),
  110. field.Time("window_7d_start").
  111. Optional().
  112. Nillable().
  113. Comment("Start time of the current 7d rate limit window"),
  114. }
  115. }
  116. func (APIKey) Edges() []ent.Edge {
  117. return []ent.Edge{
  118. edge.From("user", User.Type).
  119. Ref("api_keys").
  120. Field("user_id").
  121. Unique().
  122. Required(),
  123. edge.From("group", Group.Type).
  124. Ref("api_keys").
  125. Field("group_id").
  126. Unique(),
  127. edge.To("usage_logs", UsageLog.Type),
  128. }
  129. }
  130. func (APIKey) Indexes() []ent.Index {
  131. return []ent.Index{
  132. // key 字段已在 Fields() 中声明 Unique(),无需重复索引
  133. index.Fields("user_id"),
  134. index.Fields("group_id"),
  135. index.Fields("status"),
  136. index.Fields("deleted_at"),
  137. index.Fields("last_used_at"),
  138. // Index for quota queries
  139. index.Fields("quota", "quota_used"),
  140. index.Fields("expires_at"),
  141. }
  142. }