162 lines
4.9 KiB
Go
162 lines
4.9 KiB
Go
package apps
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/httpdns/httpdnsutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
|
"github.com/iwind/TeaGo/actions"
|
|
"github.com/iwind/TeaGo/maps"
|
|
"github.com/iwind/TeaGo/types"
|
|
)
|
|
|
|
type AppSettingsAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *AppSettingsAction) Init() {
|
|
this.Nav("httpdns", "app", "settings")
|
|
}
|
|
|
|
func (this *AppSettingsAction) RunGet(params struct {
|
|
AppId int64
|
|
Section string
|
|
}) {
|
|
httpdnsutils.AddLeftMenu(this.Parent())
|
|
|
|
app, err := findAppMap(this.Parent(), params.AppId)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
httpdnsutils.AddAppTabbar(this.Parent(), app.GetString("name"), app.GetInt64("id"), "settings")
|
|
|
|
section := params.Section
|
|
if len(section) == 0 {
|
|
section = "basic"
|
|
}
|
|
this.Data["activeSection"] = section
|
|
|
|
appIDStr := strconv.FormatInt(app.GetInt64("id"), 10)
|
|
this.Data["leftMenuItems"] = []maps.Map{
|
|
{
|
|
"name": "基础配置",
|
|
"url": "/httpdns/apps/app/settings?appId=" + appIDStr + "§ion=basic",
|
|
"isActive": section == "basic",
|
|
},
|
|
{
|
|
"name": "认证与密钥",
|
|
"url": "/httpdns/apps/app/settings?appId=" + appIDStr + "§ion=auth",
|
|
"isActive": section == "auth",
|
|
},
|
|
}
|
|
|
|
clusterResp, err := this.RPC().HTTPDNSClusterRPC().FindAllHTTPDNSClusters(this.AdminContext(), &pb.FindAllHTTPDNSClustersRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
clusters := make([]maps.Map, 0, len(clusterResp.GetClusters()))
|
|
clusterDomainMap := map[int64]string{}
|
|
clusterNameMap := map[int64]string{}
|
|
defaultPrimaryClusterId := int64(0)
|
|
for _, cluster := range clusterResp.GetClusters() {
|
|
clusterId := cluster.GetId()
|
|
clusterName := cluster.GetName()
|
|
clusters = append(clusters, maps.Map{
|
|
"id": clusterId,
|
|
"name": clusterName,
|
|
"serviceDomain": cluster.GetServiceDomain(),
|
|
"isDefault": cluster.GetIsDefault(),
|
|
})
|
|
clusterDomainMap[clusterId] = cluster.GetServiceDomain()
|
|
clusterNameMap[clusterId] = clusterName
|
|
if defaultPrimaryClusterId <= 0 && cluster.GetIsDefault() {
|
|
defaultPrimaryClusterId = clusterId
|
|
}
|
|
}
|
|
|
|
defaultBackupClusterId := int64(0)
|
|
defaultBackupResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{
|
|
Code: string(systemconfigs.SettingCodeHTTPDNSDefaultBackupClusterId),
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if defaultBackupResp != nil && len(defaultBackupResp.GetValueJSON()) > 0 {
|
|
defaultBackupClusterId = types.Int64(string(defaultBackupResp.GetValueJSON()))
|
|
}
|
|
|
|
primaryClusterId := app.GetInt64("primaryClusterId")
|
|
backupClusterId := app.GetInt64("backupClusterId")
|
|
|
|
settings := maps.Map{
|
|
"appId": app.GetString("appId"),
|
|
"appStatus": app.GetBool("isOn"),
|
|
"primaryClusterId": primaryClusterId,
|
|
"backupClusterId": backupClusterId,
|
|
"defaultPrimaryClusterId": defaultPrimaryClusterId,
|
|
"defaultPrimaryClusterName": clusterNameMap[defaultPrimaryClusterId],
|
|
"defaultBackupClusterId": defaultBackupClusterId,
|
|
"defaultBackupClusterName": clusterNameMap[defaultBackupClusterId],
|
|
"primaryServiceDomain": clusterDomainMap[primaryClusterId],
|
|
"backupServiceDomain": clusterDomainMap[backupClusterId],
|
|
"signEnabled": app.GetBool("signEnabled"),
|
|
"signSecretPlain": app.GetString("signSecretPlain"),
|
|
"signSecretMasked": app.GetString("signSecretMasked"),
|
|
"signSecretUpdatedAt": app.GetString("signSecretUpdated"),
|
|
}
|
|
this.Data["app"] = app
|
|
this.Data["settings"] = settings
|
|
this.Data["clusters"] = clusters
|
|
this.Show()
|
|
}
|
|
|
|
func (this *AppSettingsAction) RunPost(params struct {
|
|
AppId int64
|
|
|
|
AppStatus bool
|
|
PrimaryClusterId int64
|
|
BackupClusterId int64
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
params.Must.Field("appId", params.AppId).Gt(0, "请选择应用")
|
|
if params.PrimaryClusterId > 0 && params.BackupClusterId > 0 && params.PrimaryClusterId == params.BackupClusterId {
|
|
this.FailField("backupClusterId", "备用集群不能与主集群相同")
|
|
return
|
|
}
|
|
|
|
appResp, err := this.RPC().HTTPDNSAppRPC().FindHTTPDNSApp(this.AdminContext(), &pb.FindHTTPDNSAppRequest{
|
|
AppDbId: params.AppId,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if appResp.GetApp() == nil {
|
|
this.Fail("找不到对应的应用")
|
|
return
|
|
}
|
|
|
|
_, err = this.RPC().HTTPDNSAppRPC().UpdateHTTPDNSApp(this.AdminContext(), &pb.UpdateHTTPDNSAppRequest{
|
|
AppDbId: params.AppId,
|
|
Name: appResp.GetApp().GetName(),
|
|
PrimaryClusterId: params.PrimaryClusterId,
|
|
BackupClusterId: params.BackupClusterId,
|
|
IsOn: params.AppStatus,
|
|
UserId: appResp.GetApp().GetUserId(),
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|