router.go 2.5 KB

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