2020-10-25 21:27:28 +08:00
package clusterutils
2020-09-06 16:19:34 +08:00
import (
2021-04-18 15:17:49 +08:00
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
2020-12-17 17:35:38 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
2020-09-06 16:19:34 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2023-06-28 16:18:52 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
2020-12-23 09:52:31 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
2020-10-02 17:22:24 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2020-09-06 16:19:34 +08:00
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"net/http"
"strconv"
)
2022-04-01 16:42:08 +08:00
// ClusterHelper 单个集群的帮助
2020-09-06 16:19:34 +08:00
type ClusterHelper struct {
2023-06-28 16:18:52 +08:00
helpers . LangHelper
2020-09-06 16:19:34 +08:00
}
func NewClusterHelper ( ) * ClusterHelper {
return & ClusterHelper { }
}
2021-05-03 11:32:59 +08:00
func ( this * ClusterHelper ) BeforeAction ( actionPtr actions . ActionWrapper ) ( goNext bool ) {
2022-07-03 22:09:27 +08:00
var action = actionPtr . Object ( )
2020-09-06 16:19:34 +08:00
if action . Request . Method != http . MethodGet {
2021-05-03 15:31:59 +08:00
return true
2020-09-06 16:19:34 +08:00
}
action . Data [ "teaMenu" ] = "clusters"
2023-05-27 17:05:17 +08:00
var selectedTabbar = action . Data . GetString ( "mainTab" )
var clusterId = action . ParamInt64 ( "clusterId" )
var clusterIdString = strconv . FormatInt ( clusterId , 10 )
2020-09-06 16:19:34 +08:00
action . Data [ "clusterId" ] = clusterId
2020-10-26 21:14:26 +08:00
if clusterId > 0 {
2021-07-06 20:06:20 +08:00
var ctx = actionPtr . ( actionutils . ActionInterface ) . AdminContext ( )
2021-06-27 21:59:06 +08:00
cluster , err := dao . SharedNodeClusterDAO . FindEnabledNodeCluster ( ctx , clusterId )
2020-10-26 21:14:26 +08:00
if err != nil {
logs . Error ( err )
return
}
if cluster == nil {
action . WriteString ( "can not find cluster" )
return
}
2021-09-14 11:36:08 +08:00
action . Data [ "currentClusterName" ] = cluster . Name
2020-09-06 16:19:34 +08:00
2021-06-27 21:59:06 +08:00
clusterInfo , err := dao . SharedNodeClusterDAO . FindEnabledNodeClusterConfigInfo ( ctx , clusterId )
if err != nil {
logs . Error ( err )
return
}
if clusterInfo == nil {
action . WriteString ( "can not find cluster info" )
return
}
2023-05-27 17:05:17 +08:00
var nodeId = action . ParamInt64 ( "nodeId" )
var isInCluster = nodeId <= 0
2022-07-03 22:09:27 +08:00
var tabbar = actionutils . NewTabbar ( )
2023-05-27 17:11:18 +08:00
{
var url = "/clusters"
if ! isInCluster {
url = "/clusters/cluster/nodes?clusterId=" + clusterIdString
}
tabbar . Add ( "" , "" , url , "arrow left" , false )
}
2023-05-25 17:29:55 +08:00
{
2023-05-27 17:05:17 +08:00
var url = "/clusters/cluster?clusterId=" + clusterIdString
if ! isInCluster {
url = "/clusters/cluster/nodes?clusterId=" + clusterIdString
}
var item = tabbar . Add ( cluster . Name , "" , url , "angle right" , true )
item . IsTitle = true
2023-05-25 17:29:55 +08:00
}
2021-07-05 11:38:07 +08:00
if teaconst . IsPlus {
2023-05-27 17:05:17 +08:00
{
2023-06-30 18:08:30 +08:00
var item = tabbar . Add ( this . Lang ( actionPtr , codes . NodeClusterMenu_TabClusterDashboard ) , "" , "/clusters/cluster/boards?clusterId=" + clusterIdString , "chart line area" , selectedTabbar == "board" )
2023-05-27 17:05:17 +08:00
item . IsDisabled = ! isInCluster
}
}
{
2023-06-30 18:08:30 +08:00
var item = tabbar . Add ( this . Lang ( actionPtr , codes . NodeClusterMenu_TabClusterNodes ) , "" , "/clusters/cluster/nodes?clusterId=" + clusterIdString , "server" , selectedTabbar == "node" )
2023-05-27 17:05:17 +08:00
item . IsDisabled = ! isInCluster
}
{
2023-06-30 18:08:30 +08:00
var item = tabbar . Add ( this . Lang ( actionPtr , codes . NodeClusterMenu_TabClusterSettings ) , "" , "/clusters/cluster/settings?clusterId=" + clusterIdString , "setting" , selectedTabbar == "setting" )
2023-05-27 17:05:17 +08:00
item . IsDisabled = ! isInCluster
}
{
2023-06-30 18:08:30 +08:00
var item = tabbar . Add ( this . Lang ( actionPtr , codes . NodeClusterMenu_TabClusterDelete ) , "" , "/clusters/cluster/delete?clusterId=" + clusterIdString , "trash" , selectedTabbar == "delete" )
2023-05-27 17:05:17 +08:00
item . IsDisabled = ! isInCluster
2021-07-05 11:38:07 +08:00
}
2020-10-26 21:14:26 +08:00
actionutils . SetTabbar ( action , tabbar )
2020-09-06 16:19:34 +08:00
2020-10-26 21:14:26 +08:00
// 左侧菜单
2023-03-01 11:38:21 +08:00
var secondMenuItem = action . Data . GetString ( "secondMenuItem" )
2020-10-26 21:14:26 +08:00
switch selectedTabbar {
case "setting" :
2023-06-28 16:18:52 +08:00
var menuItems = this . createSettingMenu ( cluster , clusterInfo , secondMenuItem , actionPtr )
2022-04-02 16:28:22 +08:00
action . Data [ "leftMenuItems" ] = menuItems
// 当前菜单
action . Data [ "leftMenuActiveItem" ] = nil
for _ , item := range menuItems {
if item . GetBool ( "isActive" ) {
action . Data [ "leftMenuActiveItem" ] = item
break
}
}
2020-10-26 21:14:26 +08:00
}
2020-09-06 16:19:34 +08:00
}
2021-05-03 11:32:59 +08:00
return true
2020-09-06 16:19:34 +08:00
}
// 设置菜单
2023-06-28 16:18:52 +08:00
func ( this * ClusterHelper ) createSettingMenu ( cluster * pb . NodeCluster , info * pb . FindEnabledNodeClusterConfigInfoResponse , selectedItem string , actionPtr actions . ActionWrapper ) ( items [ ] maps . Map ) {
2020-12-17 17:35:38 +08:00
clusterId := numberutils . FormatInt64 ( cluster . Id )
2020-09-06 16:19:34 +08:00
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingBasic ) ,
2020-09-06 16:19:34 +08:00
"url" : "/clusters/cluster/settings?clusterId=" + clusterId ,
"isActive" : selectedItem == "basic" ,
2022-04-22 22:04:36 +08:00
"isOn" : true ,
2020-09-06 16:19:34 +08:00
} )
2023-05-23 19:50:19 +08:00
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingDNS ) ,
2023-05-23 19:50:19 +08:00
"url" : "/clusters/cluster/settings/dns?clusterId=" + clusterId ,
"isActive" : selectedItem == "dns" ,
"isOn" : cluster . DnsDomainId > 0 || len ( cluster . DnsName ) > 0 ,
} )
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingHealthCheck ) ,
2023-05-23 19:50:19 +08:00
"url" : "/clusters/cluster/settings/health?clusterId=" + clusterId ,
"isActive" : selectedItem == "health" ,
"isOn" : info != nil && info . HealthCheckIsOn ,
} )
items = append ( items , maps . Map {
"name" : "-" ,
} )
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingServiceGlobal ) ,
2023-05-23 19:50:19 +08:00
"url" : "/clusters/cluster/settings/global-server-config?clusterId=" + clusterId ,
"isActive" : selectedItem == "globalServerConfig" ,
"isOn" : true ,
} )
2020-12-17 15:50:44 +08:00
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingCachePolicy ) ,
2020-12-17 15:50:44 +08:00
"url" : "/clusters/cluster/settings/cache?clusterId=" + clusterId ,
"isActive" : selectedItem == "cache" ,
2020-12-17 17:35:38 +08:00
"isOn" : cluster . HttpCachePolicyId > 0 ,
2020-12-17 15:50:44 +08:00
} )
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingWAFPolicy ) ,
2020-12-17 15:50:44 +08:00
"url" : "/clusters/cluster/settings/waf?clusterId=" + clusterId ,
"isActive" : selectedItem == "waf" ,
2020-12-17 17:35:38 +08:00
"isOn" : cluster . HttpFirewallPolicyId > 0 ,
2020-12-17 15:50:44 +08:00
} )
2021-02-24 15:01:45 +08:00
2021-06-27 21:59:06 +08:00
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingWAFActions ) ,
2021-06-27 21:59:06 +08:00
"url" : "/clusters/cluster/settings/firewall-actions?clusterId=" + clusterId ,
"isActive" : selectedItem == "firewallAction" ,
"isOn" : info != nil && info . HasFirewallActions ,
} )
2021-02-24 15:01:45 +08:00
2022-05-18 21:02:47 +08:00
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingWebP ) ,
2022-07-04 10:31:25 +08:00
"url" : "/clusters/cluster/settings/webp?clusterId=" + clusterId ,
"isActive" : selectedItem == "webp" ,
2023-12-11 11:07:02 +08:00
"isOn" : info != nil && info . WebPIsOn ,
2022-07-04 10:31:25 +08:00
} )
2023-06-28 16:18:52 +08:00
items = this . filterMenuItems1 ( items , info , clusterId , selectedItem , actionPtr )
2022-07-04 10:31:25 +08:00
2021-06-30 19:59:59 +08:00
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingMetrics ) ,
2021-06-27 21:59:06 +08:00
"url" : "/clusters/cluster/settings/metrics?clusterId=" + clusterId ,
"isActive" : selectedItem == "metric" ,
"isOn" : info != nil && info . HasMetricItems ,
2021-06-30 19:59:59 +08:00
} )
2021-06-27 21:59:06 +08:00
2021-04-12 19:19:59 +08:00
items = append ( items , maps . Map {
"name" : "-" ,
"url" : "" ,
"isActive" : false ,
} )
2021-01-11 18:15:53 +08:00
items = append ( items , maps . Map {
2023-06-30 18:08:30 +08:00
"name" : this . Lang ( actionPtr , codes . NodeClusterMenu_SettingDDoSProtection ) ,
2023-06-24 17:22:08 +08:00
"url" : "/clusters/cluster/settings/ddos-protection?clusterId=" + clusterId ,
"isActive" : selectedItem == "ddosProtection" ,
"isOn" : info != nil && info . HasDDoSProtection ,
2021-01-11 18:15:53 +08:00
} )
2023-06-24 17:22:08 +08:00
2023-06-28 16:18:52 +08:00
items = this . filterMenuItems2 ( items , info , clusterId , selectedItem , actionPtr )
2023-06-24 17:22:08 +08:00
2020-09-06 16:19:34 +08:00
return
}