Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 4bb8ab6

Browse files
committed
修改左侧菜单分组
1 parent 4ca7096 commit 4bb8ab6

3 files changed

Lines changed: 113 additions & 63 deletions

File tree

teaconfigs/server_group.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ func (this *ServerGroup) Remove(serverId string) {
3939
}
4040
this.ServerIds = result
4141
}
42+
43+
func (this *ServerGroup) Contains(serverId string) bool {
44+
return lists.ContainsString(this.ServerIds, serverId)
45+
}

teaweb/actions/default/proxy/index.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@ type IndexAction actions.Action
1111
// 代理首页
1212
func (this *IndexAction) Run(params struct {
1313
}) {
14-
// 跳转到第一个
14+
// 跳转到分组的第一个
15+
groupList := teaconfigs.SharedServerGroupList()
16+
serverId := ""
17+
for _, group := range groupList.Groups {
18+
if !group.IsOn {
19+
continue
20+
}
21+
if len(group.ServerIds) == 0 {
22+
continue
23+
}
24+
serverId = group.ServerIds[0]
25+
break
26+
}
27+
if len(serverId) > 0 {
28+
this.RedirectURL("/proxy/board?serverId=" + serverId)
29+
return
30+
}
31+
32+
// 没有分组,就跳到所有的服务的第一个
1533
serverList, err := teaconfigs.SharedServerList()
1634
if err != nil {
1735
logs.Error(err)

teaweb/actions/default/proxy/proxyutils/menu.go

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,87 +20,54 @@ func AddServerMenu(actionWrapper actions.ActionWrapper, isIndex bool) {
2020
menuGroup := utils.NewMenuGroup()
2121

2222
// 服务
23-
var hasServer = false
2423
var isTCP = false
2524

2625
serverId := action.ParamString("serverId")
2726
serverList, err := teaconfigs.SharedServerList()
2827
if err != nil {
2928
logs.Error(err)
29+
return
3030
}
31+
var hasServer = len(serverList.Files) > 0
3132

3233
if isIndex {
33-
menu := menuGroup.FindMenu("", "")
34-
for _, server := range serverList.FindAllServers() {
35-
urlPrefix := "/proxy/board"
36-
if server.IsTCP() { // TCP
37-
urlPrefix = "/proxy/detail"
38-
} else { // HTTP
39-
if action.HasPrefix("/proxy/stat") {
40-
urlPrefix = "/proxy/stat"
41-
} else if action.HasPrefix("/proxy/log") {
42-
urlPrefix = "/proxy/log"
43-
} else if action.HasPrefix("/proxy") && !action.HasPrefix("/proxy/board", "/proxy/add") {
44-
urlPrefix = "/proxy/detail"
45-
}
34+
allServers := serverList.FindAllServers()
35+
for _, server := range allServers {
36+
if server.Id == serverId {
37+
isTCP = server.IsTCP()
4638
}
39+
}
4740

48-
item := menu.Add(server.Description, "", urlPrefix+"?serverId="+server.Id, serverId == server.Id)
49-
item.IsSortable = true
41+
// 分组列表
42+
groupList := teaconfigs.SharedServerGroupList()
5043

51-
if server.IsTCP() {
52-
item.SupName = "tcp"
53-
} else if server.ForwardHTTP != nil {
54-
item.SupName = "forward"
44+
// 已分组
45+
for _, group := range groupList.Groups {
46+
if len(group.ServerIds) == 0 {
47+
continue
5548
}
5649

57-
// port
58-
ports := []string{}
59-
if server.Http {
60-
for _, listen := range server.Listen {
61-
index := strings.LastIndex(listen, ":")
62-
if index > -1 {
63-
ports = append(ports, ":"+listen[index+1:])
64-
} else {
65-
ports = append(ports, ":"+listen)
66-
}
67-
}
68-
}
69-
if server.SSL != nil && server.SSL.On {
70-
for _, listen := range server.SSL.Listen {
71-
index := strings.LastIndex(listen, ":")
72-
if index > -1 {
73-
ports = append(ports, ":"+listen[index+1:])
74-
} else {
75-
ports = append(ports, ":"+listen)
76-
}
77-
}
78-
}
79-
if len(ports) > 0 {
80-
if len(ports) > 2 {
81-
item.SubName = ports[0] + ", " + ports[1] + "等 "
82-
} else {
83-
item.SubName = strings.Join(ports, ", ") + " "
84-
}
85-
}
50+
menu := menuGroup.FindMenu("group_"+group.Id, group.Name)
8651

87-
// on | off
88-
if (server.IsHTTP() && !server.Http && (server.SSL == nil || !server.SSL.On)) || (server.IsTCP() && (server.TCP == nil || !server.TCP.TCPOn)) {
89-
item.SubName = "未启用"
90-
item.SubColor = "red"
52+
for _, server := range allServers {
53+
if !group.Contains(server.Id) {
54+
continue
55+
}
56+
AddServerToMenu(server, action, menu, serverId)
9157
}
58+
}
9259

93-
if server.Id == serverId {
94-
isTCP = server.IsTCP()
60+
// 是否有未分组
61+
unGroupServers := []*teaconfigs.ServerConfig{}
62+
for _, server := range allServers {
63+
if !groupList.ContainsServer(server.Id) {
64+
unGroupServers = append(unGroupServers, server)
9565
}
96-
97-
hasServer = true
9866
}
99-
if hasServer {
100-
if action.Request.URL.Path == "/proxy/board" {
101-
menu.Name = "代理服务"
102-
} else {
103-
menu.Name = "代理服务"
67+
if len(unGroupServers) > 0 {
68+
menu := menuGroup.FindMenu("", "[未分组]")
69+
for _, server := range unGroupServers {
70+
AddServerToMenu(server, action, menu, serverId)
10471
}
10572
}
10673
}
@@ -203,6 +170,67 @@ func AddServerMenu(actionWrapper actions.ActionWrapper, isIndex bool) {
203170
}
204171
}
205172

173+
// 将Server添加到菜单上
174+
func AddServerToMenu(server *teaconfigs.ServerConfig, action *actions.ActionObject, menu *utils.Menu, serverId string) {
175+
urlPrefix := "/proxy/board"
176+
if server.IsTCP() { // TCP
177+
urlPrefix = "/proxy/detail"
178+
} else { // HTTP
179+
if action.HasPrefix("/proxy/stat") {
180+
urlPrefix = "/proxy/stat"
181+
} else if action.HasPrefix("/proxy/log") {
182+
urlPrefix = "/proxy/log"
183+
} else if action.HasPrefix("/proxy") && !action.HasPrefix("/proxy/board", "/proxy/add") {
184+
urlPrefix = "/proxy/detail"
185+
}
186+
}
187+
188+
item := menu.Add(server.Description, "", urlPrefix+"?serverId="+server.Id, serverId == server.Id)
189+
item.IsSortable = true
190+
191+
if server.IsTCP() {
192+
item.SupName = "tcp"
193+
} else if server.ForwardHTTP != nil {
194+
item.SupName = "forward"
195+
}
196+
197+
// port
198+
ports := []string{}
199+
if server.Http {
200+
for _, listen := range server.Listen {
201+
index := strings.LastIndex(listen, ":")
202+
if index > -1 {
203+
ports = append(ports, ":"+listen[index+1:])
204+
} else {
205+
ports = append(ports, ":"+listen)
206+
}
207+
}
208+
}
209+
if server.SSL != nil && server.SSL.On {
210+
for _, listen := range server.SSL.Listen {
211+
index := strings.LastIndex(listen, ":")
212+
if index > -1 {
213+
ports = append(ports, ":"+listen[index+1:])
214+
} else {
215+
ports = append(ports, ":"+listen)
216+
}
217+
}
218+
}
219+
if len(ports) > 0 {
220+
if len(ports) > 2 {
221+
item.SubName = ports[0] + ", " + ports[1] + "等 "
222+
} else {
223+
item.SubName = strings.Join(ports, ", ") + " "
224+
}
225+
}
226+
227+
// on | off
228+
if (server.IsHTTP() && !server.Http && (server.SSL == nil || !server.SSL.On)) || (server.IsTCP() && (server.TCP == nil || !server.TCP.TCPOn)) {
229+
item.SubName = "未启用"
230+
item.SubColor = "red"
231+
}
232+
}
233+
206234
// 包装Server相关数据
207235
func WrapServerData(server *teaconfigs.ServerConfig) maps.Map {
208236
return maps.Map{

0 commit comments

Comments
 (0)