router.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package vhost
  2. import (
  3. "cmp"
  4. "errors"
  5. "slices"
  6. "strings"
  7. "sync"
  8. )
  9. var ErrRouterConfigConflict = errors.New("router config conflict")
  10. type routerByHTTPUser map[string][]*Router
  11. type Routers struct {
  12. indexByDomain map[string]routerByHTTPUser
  13. mutex sync.RWMutex
  14. }
  15. type Router struct {
  16. domain string
  17. location string
  18. httpUser string
  19. // store any object here
  20. payload interface{}
  21. }
  22. func NewRouters() *Routers {
  23. return &Routers{
  24. indexByDomain: make(map[string]routerByHTTPUser),
  25. }
  26. }
  27. func (r *Routers) Add(domain, location, httpUser string, payload interface{}) error {
  28. domain = strings.ToLower(domain)
  29. r.mutex.Lock()
  30. defer r.mutex.Unlock()
  31. if _, exist := r.exist(domain, location, httpUser); exist {
  32. return ErrRouterConfigConflict
  33. }
  34. routersByHTTPUser, found := r.indexByDomain[domain]
  35. if !found {
  36. routersByHTTPUser = make(map[string][]*Router)
  37. }
  38. vrs, found := routersByHTTPUser[httpUser]
  39. if !found {
  40. vrs = make([]*Router, 0, 1)
  41. }
  42. vr := &Router{
  43. domain: domain,
  44. location: location,
  45. httpUser: httpUser,
  46. payload: payload,
  47. }
  48. vrs = append(vrs, vr)
  49. slices.SortFunc(vrs, func(a, b *Router) int {
  50. return -cmp.Compare(a.location, b.location)
  51. })
  52. routersByHTTPUser[httpUser] = vrs
  53. r.indexByDomain[domain] = routersByHTTPUser
  54. return nil
  55. }
  56. func (r *Routers) Del(domain, location, httpUser string) {
  57. domain = strings.ToLower(domain)
  58. r.mutex.Lock()
  59. defer r.mutex.Unlock()
  60. routersByHTTPUser, found := r.indexByDomain[domain]
  61. if !found {
  62. return
  63. }
  64. vrs, found := routersByHTTPUser[httpUser]
  65. if !found {
  66. return
  67. }
  68. newVrs := make([]*Router, 0)
  69. for _, vr := range vrs {
  70. if vr.location != location {
  71. newVrs = append(newVrs, vr)
  72. }
  73. }
  74. routersByHTTPUser[httpUser] = newVrs
  75. }
  76. func (r *Routers) Get(host, path, httpUser string) (vr *Router, exist bool) {
  77. host = strings.ToLower(host)
  78. r.mutex.RLock()
  79. defer r.mutex.RUnlock()
  80. routersByHTTPUser, found := r.indexByDomain[host]
  81. if !found {
  82. return
  83. }
  84. vrs, found := routersByHTTPUser[httpUser]
  85. if !found {
  86. return
  87. }
  88. for _, vr = range vrs {
  89. if strings.HasPrefix(path, vr.location) {
  90. return vr, true
  91. }
  92. }
  93. return
  94. }
  95. func (r *Routers) exist(host, path, httpUser string) (route *Router, exist bool) {
  96. routersByHTTPUser, found := r.indexByDomain[host]
  97. if !found {
  98. return
  99. }
  100. routers, found := routersByHTTPUser[httpUser]
  101. if !found {
  102. return
  103. }
  104. for _, route = range routers {
  105. if path == route.location {
  106. return route, true
  107. }
  108. }
  109. return
  110. }