2016-12-18 14:40:58 +00:00
|
|
|
package vhost
|
|
|
|
|
|
|
|
import (
|
2019-07-30 16:41:58 +00:00
|
|
|
"errors"
|
2016-12-18 14:40:58 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-08-28 17:02:53 +00:00
|
|
|
var ErrRouterConfigConflict = errors.New("router config conflict")
|
2019-07-30 16:41:58 +00:00
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
type routerByHTTPUser map[string][]*Router
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
type Routers struct {
|
2022-05-26 15:57:30 +00:00
|
|
|
indexByDomain map[string]routerByHTTPUser
|
|
|
|
|
|
|
|
mutex sync.RWMutex
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
type Router struct {
|
2016-12-18 14:40:58 +00:00
|
|
|
domain string
|
|
|
|
location string
|
2022-05-26 15:57:30 +00:00
|
|
|
httpUser string
|
2017-12-12 19:27:43 +00:00
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
// store any object here
|
2017-12-12 19:27:43 +00:00
|
|
|
payload interface{}
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
func NewRouters() *Routers {
|
|
|
|
return &Routers{
|
2022-05-26 15:57:30 +00:00
|
|
|
indexByDomain: make(map[string]routerByHTTPUser),
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
func (r *Routers) Add(domain, location, httpUser string, payload interface{}) error {
|
2016-12-18 14:40:58 +00:00
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
if _, exist := r.exist(domain, location, httpUser); exist {
|
2019-07-30 16:41:58 +00:00
|
|
|
return ErrRouterConfigConflict
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
routersByHTTPUser, found := r.indexByDomain[domain]
|
|
|
|
if !found {
|
|
|
|
routersByHTTPUser = make(map[string][]*Router)
|
|
|
|
}
|
|
|
|
vrs, found := routersByHTTPUser[httpUser]
|
2016-12-18 14:40:58 +00:00
|
|
|
if !found {
|
2020-05-24 09:48:37 +00:00
|
|
|
vrs = make([]*Router, 0, 1)
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
vr := &Router{
|
2016-12-24 17:53:23 +00:00
|
|
|
domain: domain,
|
|
|
|
location: location,
|
2022-05-26 15:57:30 +00:00
|
|
|
httpUser: httpUser,
|
2017-12-12 19:27:43 +00:00
|
|
|
payload: payload,
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
2016-12-24 17:53:23 +00:00
|
|
|
vrs = append(vrs, vr)
|
2016-12-26 18:52:32 +00:00
|
|
|
sort.Sort(sort.Reverse(ByLocation(vrs)))
|
2022-05-26 15:57:30 +00:00
|
|
|
|
|
|
|
routersByHTTPUser[httpUser] = vrs
|
|
|
|
r.indexByDomain[domain] = routersByHTTPUser
|
2019-07-30 16:41:58 +00:00
|
|
|
return nil
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
func (r *Routers) Del(domain, location, httpUser string) {
|
2016-12-18 14:40:58 +00:00
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
routersByHTTPUser, found := r.indexByDomain[domain]
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vrs, found := routersByHTTPUser[httpUser]
|
2016-12-18 14:40:58 +00:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 09:48:37 +00:00
|
|
|
newVrs := make([]*Router, 0)
|
2018-05-22 15:59:35 +00:00
|
|
|
for _, vr := range vrs {
|
|
|
|
if vr.location != location {
|
|
|
|
newVrs = append(newVrs, vr)
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-26 15:57:30 +00:00
|
|
|
routersByHTTPUser[httpUser] = newVrs
|
2016-12-18 14:40:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
func (r *Routers) Get(host, path, httpUser string) (vr *Router, exist bool) {
|
2016-12-18 14:40:58 +00:00
|
|
|
r.mutex.RLock()
|
|
|
|
defer r.mutex.RUnlock()
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
routersByHTTPUser, found := r.indexByDomain[host]
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vrs, found := routersByHTTPUser[httpUser]
|
2016-12-19 02:34:37 +00:00
|
|
|
if !found {
|
2016-12-18 14:40:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vr = range vrs {
|
2016-12-24 17:53:23 +00:00
|
|
|
if strings.HasPrefix(path, vr.location) {
|
2016-12-18 14:40:58 +00:00
|
|
|
return vr, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-12-24 17:53:23 +00:00
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
func (r *Routers) exist(host, path, httpUser string) (route *Router, exist bool) {
|
|
|
|
routersByHTTPUser, found := r.indexByDomain[host]
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
routers, found := routersByHTTPUser[httpUser]
|
2016-12-26 17:01:39 +00:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:57:30 +00:00
|
|
|
for _, route = range routers {
|
|
|
|
if path == route.location {
|
|
|
|
return route, true
|
2016-12-26 17:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by location
|
2020-05-24 09:48:37 +00:00
|
|
|
type ByLocation []*Router
|
2016-12-24 17:53:23 +00:00
|
|
|
|
|
|
|
func (a ByLocation) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
2022-08-28 17:02:53 +00:00
|
|
|
|
2016-12-24 17:53:23 +00:00
|
|
|
func (a ByLocation) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
2022-08-28 17:02:53 +00:00
|
|
|
|
2016-12-24 17:53:23 +00:00
|
|
|
func (a ByLocation) Less(i, j int) bool {
|
|
|
|
return strings.Compare(a[i].location, a[j].location) < 0
|
|
|
|
}
|