2
0

request_filter.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package model
  2. import (
  3. "time"
  4. "github.com/uptrace/bun"
  5. )
  6. // RequestFilter 请求过滤器模型
  7. type RequestFilter struct {
  8. bun.BaseModel `bun:"table:request_filters,alias:rf"`
  9. ID int `bun:"id,pk,autoincrement" json:"id"`
  10. Name string `bun:"name,notnull" json:"name"`
  11. Description *string `bun:"description" json:"description"`
  12. // 作用域:header 或 body
  13. Scope string `bun:"scope,notnull" json:"scope"` // header, body
  14. // 动作类型
  15. Action string `bun:"action,notnull" json:"action"` // remove, set, json_path, text_replace
  16. // 匹配类型(可选)
  17. MatchType *string `bun:"match_type" json:"matchType"`
  18. // 目标(要匹配/操作的字段或路径)
  19. Target string `bun:"target,notnull" json:"target"`
  20. // 替换值(JSONB)
  21. Replacement interface{} `bun:"replacement,type:jsonb" json:"replacement"`
  22. // 优先级
  23. Priority int `bun:"priority,notnull,default:0" json:"priority"`
  24. // 是否启用
  25. IsEnabled bool `bun:"is_enabled,notnull,default:true" json:"isEnabled"`
  26. // 绑定类型
  27. BindingType string `bun:"binding_type,notnull,default:'global'" json:"bindingType"` // global, providers, groups
  28. // 绑定的供应商 ID 列表
  29. ProviderIds []int `bun:"provider_ids,type:jsonb" json:"providerIds"`
  30. // 绑定的分组标签列表
  31. GroupTags []string `bun:"group_tags,type:jsonb" json:"groupTags"`
  32. CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp" json:"createdAt"`
  33. UpdatedAt time.Time `bun:"updated_at,notnull,default:current_timestamp" json:"updatedAt"`
  34. }
  35. // IsActive 检查请求过滤器是否处于活跃状态
  36. func (r *RequestFilter) IsActive() bool {
  37. return r.IsEnabled
  38. }
  39. // IsGlobal 检查是否为全局过滤器
  40. func (r *RequestFilter) IsGlobal() bool {
  41. return r.BindingType == string(RequestFilterBindingTypeGlobal)
  42. }
  43. // AppliesToProvider 检查过滤器是否适用于指定供应商
  44. func (r *RequestFilter) AppliesToProvider(providerID int, groupTag *string) bool {
  45. if r.IsGlobal() {
  46. return true
  47. }
  48. if r.BindingType == string(RequestFilterBindingTypeProviders) {
  49. for _, id := range r.ProviderIds {
  50. if id == providerID {
  51. return true
  52. }
  53. }
  54. }
  55. if r.BindingType == string(RequestFilterBindingTypeGroups) && groupTag != nil {
  56. for _, tag := range r.GroupTags {
  57. if tag == *groupTag {
  58. return true
  59. }
  60. }
  61. }
  62. return false
  63. }