router.go 2.5 KB

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