2016-12-18 15:40:58 +01:00
|
|
|
package vhost
|
|
|
|
|
|
|
|
import (
|
2019-07-30 18:41:58 +02:00
|
|
|
"errors"
|
2016-12-18 15:40:58 +01:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-07-30 18:41:58 +02:00
|
|
|
var (
|
|
|
|
ErrRouterConfigConflict = errors.New("router config conflict")
|
|
|
|
)
|
|
|
|
|
2016-12-18 15:40:58 +01:00
|
|
|
type VhostRouters struct {
|
|
|
|
RouterByDomain map[string][]*VhostRouter
|
|
|
|
mutex sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type VhostRouter struct {
|
|
|
|
domain string
|
|
|
|
location string
|
2017-12-12 20:27:43 +01:00
|
|
|
|
|
|
|
payload interface{}
|
2016-12-18 15:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewVhostRouters() *VhostRouters {
|
|
|
|
return &VhostRouters{
|
|
|
|
RouterByDomain: make(map[string][]*VhostRouter),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:41:58 +02:00
|
|
|
func (r *VhostRouters) Add(domain, location string, payload interface{}) error {
|
2016-12-18 15:40:58 +01:00
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
|
2019-07-30 18:41:58 +02:00
|
|
|
if _, exist := r.exist(domain, location); exist {
|
|
|
|
return ErrRouterConfigConflict
|
|
|
|
}
|
|
|
|
|
2016-12-18 15:40:58 +01:00
|
|
|
vrs, found := r.RouterByDomain[domain]
|
|
|
|
if !found {
|
2016-12-26 19:52:32 +01:00
|
|
|
vrs = make([]*VhostRouter, 0, 1)
|
2016-12-18 15:40:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-24 18:53:23 +01:00
|
|
|
vr := &VhostRouter{
|
|
|
|
domain: domain,
|
|
|
|
location: location,
|
2017-12-12 20:27:43 +01:00
|
|
|
payload: payload,
|
2016-12-18 15:40:58 +01:00
|
|
|
}
|
2016-12-24 18:53:23 +01:00
|
|
|
vrs = append(vrs, vr)
|
2016-12-18 15:40:58 +01:00
|
|
|
|
2016-12-26 19:52:32 +01:00
|
|
|
sort.Sort(sort.Reverse(ByLocation(vrs)))
|
2016-12-18 15:40:58 +01:00
|
|
|
r.RouterByDomain[domain] = vrs
|
2019-07-30 18:41:58 +02:00
|
|
|
return nil
|
2016-12-18 15:40:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 19:52:32 +01:00
|
|
|
func (r *VhostRouters) Del(domain, location string) {
|
2016-12-18 15:40:58 +01:00
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
|
2016-12-26 19:52:32 +01:00
|
|
|
vrs, found := r.RouterByDomain[domain]
|
2016-12-18 15:40:58 +01:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 17:59:35 +02:00
|
|
|
newVrs := make([]*VhostRouter, 0)
|
|
|
|
for _, vr := range vrs {
|
|
|
|
if vr.location != location {
|
|
|
|
newVrs = append(newVrs, vr)
|
2016-12-18 15:40:58 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-22 17:59:35 +02:00
|
|
|
r.RouterByDomain[domain] = newVrs
|
2016-12-18 15:40:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-24 18:53:23 +01:00
|
|
|
func (r *VhostRouters) Get(host, path string) (vr *VhostRouter, exist bool) {
|
2016-12-18 15:40:58 +01:00
|
|
|
r.mutex.RLock()
|
|
|
|
defer r.mutex.RUnlock()
|
|
|
|
|
2016-12-24 18:53:23 +01:00
|
|
|
vrs, found := r.RouterByDomain[host]
|
2016-12-19 03:34:37 +01:00
|
|
|
if !found {
|
2016-12-18 15:40:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-26 18:01:39 +01:00
|
|
|
// can't support load balance, will to do
|
2016-12-18 15:40:58 +01:00
|
|
|
for _, vr = range vrs {
|
2016-12-24 18:53:23 +01:00
|
|
|
if strings.HasPrefix(path, vr.location) {
|
2016-12-18 15:40:58 +01:00
|
|
|
return vr, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2016-12-24 18:53:23 +01:00
|
|
|
|
2019-07-30 18:41:58 +02:00
|
|
|
func (r *VhostRouters) exist(host, path string) (vr *VhostRouter, exist bool) {
|
2016-12-26 18:01:39 +01:00
|
|
|
vrs, found := r.RouterByDomain[host]
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vr = range vrs {
|
|
|
|
if path == vr.location {
|
|
|
|
return vr, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by location
|
2016-12-24 18:53:23 +01:00
|
|
|
type ByLocation []*VhostRouter
|
|
|
|
|
|
|
|
func (a ByLocation) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
func (a ByLocation) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
func (a ByLocation) Less(i, j int) bool {
|
|
|
|
return strings.Compare(a[i].location, a[j].location) < 0
|
|
|
|
}
|