forked from hidu/api-front
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_host.go
97 lines (85 loc) · 1.59 KB
/
api_host.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package proxy
import (
"time"
)
// Host one api backend host
type Host struct {
Name string `json:"-"`
URLStr string `json:"url"`
Enable bool `json:"enable"`
Note string `json:"note"`
SortIndex int `json:"sort"`
Checked bool `json:"-"`
}
// Hosts api hosts
type Hosts map[string]*Host
func newHosts() Hosts {
return make(Hosts)
}
func (h *Host) copy() *Host {
return &Host{
Name: h.Name,
URLStr: h.URLStr,
Enable: h.Enable,
Note: h.Note,
SortIndex: h.SortIndex,
}
}
func (hs Hosts) addNewHost(host *Host) {
hs[host.Name] = host
}
func (hs Hosts) init() {
for name, host := range hs {
host.Name = name
}
}
func newHost(name string, url string, enable bool) *Host {
return &Host{
Name: name,
URLStr: url,
Enable: enable,
}
}
func (hs Hosts) getDefaultHostName() string {
n := time.Now().UnixNano() % int64(len(hs))
for name := range hs {
if n == 0 {
return name
}
n = n - 1
}
return ""
}
func (hs Hosts) activeHostsNum() int {
num := 0
for _, host := range hs {
if host.Enable {
num = num + 1
}
}
return num
}
// GetHostsWithPref tpl call this
//
func (hs Hosts) GetHostsWithPref(pref []string) []*Host {
enableNames := []string{}
for name, host := range hs {
if host.Enable {
enableNames = append(enableNames, name)
}
}
var arr []*Host
for _, name := range pref {
if InStringSlice(name, enableNames) {
h := hs[name].copy()
h.Checked = true
arr = append(arr, h)
}
}
for name, host := range hs {
if host.Enable && !InStringSlice(name, pref) {
arr = append(arr, host)
}
}
return arr
}