/
dispatch.go
67 lines (54 loc) · 1.41 KB
/
dispatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package runtime
import (
"github.com/aosfather/myway/meta"
"strings"
)
/**
dispatch
*/
type SimpleDispatch struct {
defaultNode *node
}
func (this *SimpleDispatch) Init() {
this.defaultNode = &node{}
}
func (this *SimpleDispatch) AddUrl(url string, handle interface{}) {
this.defaultNode.addRoute(url, handle)
}
func (this *SimpleDispatch) GetUrl(url string) interface{} {
h, _, _ := this.defaultNode.getValue(url)
return h
}
type DispatchManager struct {
domainNode map[string]*node //特定域名下的node
defaultNode *node //默认
clusterMap map[string]*meta.ServerCluster //集群列表
apiMap map[string]*meta.Api //api列表
env meta.Env //环境列表
}
func (this *DispatchManager) Init() {
this.domainNode = make(map[string]*node)
this.clusterMap = make(map[string]*meta.ServerCluster)
this.apiMap = make(map[string]*meta.Api)
this.defaultNode = &node{}
}
//根据域名和url获取对应的API
func (this *DispatchManager) GetApi(domain, url string) *meta.Api {
node := this.domainNode[domain]
if node == nil {
node = this.defaultNode
}
if node != nil {
paramIndex := strings.Index(url, "?")
realuri := url
if paramIndex != -1 {
realuri = strings.TrimSpace((url[:paramIndex]))
}
h, _, _ := node.getValue(realuri)
if h != nil {
key := h.(string)
return this.apiMap[key]
}
}
return nil
}