proxy.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package schema
  2. import (
  3. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect/entsql"
  6. "entgo.io/ent/schema"
  7. "entgo.io/ent/schema/edge"
  8. "entgo.io/ent/schema/field"
  9. "entgo.io/ent/schema/index"
  10. )
  11. // Proxy holds the schema definition for the Proxy entity.
  12. type Proxy struct {
  13. ent.Schema
  14. }
  15. func (Proxy) Annotations() []schema.Annotation {
  16. return []schema.Annotation{
  17. entsql.Annotation{Table: "proxies"},
  18. }
  19. }
  20. func (Proxy) Mixin() []ent.Mixin {
  21. return []ent.Mixin{
  22. mixins.TimeMixin{},
  23. mixins.SoftDeleteMixin{},
  24. }
  25. }
  26. func (Proxy) Fields() []ent.Field {
  27. return []ent.Field{
  28. field.String("name").
  29. MaxLen(100).
  30. NotEmpty(),
  31. field.String("protocol").
  32. MaxLen(20).
  33. NotEmpty(),
  34. field.String("host").
  35. MaxLen(255).
  36. NotEmpty(),
  37. field.Int("port"),
  38. field.String("username").
  39. MaxLen(100).
  40. Optional().
  41. Nillable(),
  42. field.String("password").
  43. MaxLen(100).
  44. Optional().
  45. Nillable(),
  46. field.String("status").
  47. MaxLen(20).
  48. Default("active"),
  49. field.Time("expires_at").
  50. Optional().Nillable().
  51. Comment("Proxy expiration time (NULL means never expires)."),
  52. field.String("fallback_mode").
  53. MaxLen(20).Default("none").
  54. Comment("Fallback target on expiry: none | proxy | direct."),
  55. field.Int64("backup_proxy_id").
  56. Optional().Nillable().
  57. Comment("Backup proxy id when fallback_mode=proxy (self-reference)."),
  58. field.Int("expiry_warn_days").
  59. Default(7).
  60. Comment("Days before expiry to flag as expiring-soon (per proxy)."),
  61. }
  62. }
  63. // Edges 定义代理实体的关联关系。
  64. func (Proxy) Edges() []ent.Edge {
  65. return []ent.Edge{
  66. // accounts: 使用此代理的账户(反向边)
  67. edge.From("accounts", Account.Type).
  68. Ref("proxy"),
  69. // Directed many-to-one: a backup can serve multiple primary proxies.
  70. // The inverse edge prevents Ent from treating this self-reference as symmetric.
  71. edge.From("primary_proxies", Proxy.Type).
  72. Ref("backup_proxy"),
  73. edge.To("backup_proxy", Proxy.Type).
  74. Field("backup_proxy_id").
  75. Unique(),
  76. }
  77. }
  78. func (Proxy) Indexes() []ent.Index {
  79. return []ent.Index{
  80. index.Fields("status"),
  81. index.Fields("deleted_at"),
  82. index.Fields("expires_at"),
  83. index.Fields("backup_proxy_id"),
  84. }
  85. }