176 lines
4.7 KiB
Go
176 lines
4.7 KiB
Go
package apps
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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/iwind/TeaGo/actions"
|
|
"github.com/iwind/TeaGo/maps"
|
|
)
|
|
|
|
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()))
|
|
clusterApiAddressMap := map[int64]string{}
|
|
clusterNameMap := map[int64]string{}
|
|
for _, cluster := range clusterResp.GetClusters() {
|
|
clusterId := cluster.GetId()
|
|
clusterName := cluster.GetName()
|
|
|
|
port := "443"
|
|
if rawTLS := cluster.GetTlsPolicyJSON(); len(rawTLS) > 0 {
|
|
var tlsConfig map[string]interface{}
|
|
if err := json.Unmarshal(rawTLS, &tlsConfig); err == nil {
|
|
if listenRaw, ok := tlsConfig["listen"]; ok && listenRaw != nil {
|
|
if data, err := json.Marshal(listenRaw); err == nil {
|
|
var listenAddresses []map[string]interface{}
|
|
if err := json.Unmarshal(data, &listenAddresses); err == nil {
|
|
if len(listenAddresses) > 0 {
|
|
if portRange, ok := listenAddresses[0]["portRange"].(string); ok && len(portRange) > 0 {
|
|
port = portRange
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
apiAddress := "https://" + cluster.GetServiceDomain() + ":" + port
|
|
|
|
clusters = append(clusters, maps.Map{
|
|
"id": clusterId,
|
|
"name": clusterName,
|
|
})
|
|
clusterApiAddressMap[clusterId] = apiAddress
|
|
clusterNameMap[clusterId] = clusterName
|
|
}
|
|
|
|
// 读取应用绑定的集群列表,取第一个作为当前选中。
|
|
var selectedClusterId int64
|
|
if raw := app.Get("clusterIds"); raw != nil {
|
|
if ids, ok := raw.([]int64); ok && len(ids) > 0 {
|
|
selectedClusterId = ids[0]
|
|
}
|
|
}
|
|
|
|
// 构建服务地址列表。
|
|
serviceAddresses := make([]maps.Map, 0)
|
|
if selectedClusterId > 0 {
|
|
addr := clusterApiAddressMap[selectedClusterId]
|
|
name := clusterNameMap[selectedClusterId]
|
|
if len(addr) > 0 {
|
|
serviceAddresses = append(serviceAddresses, maps.Map{
|
|
"address": addr,
|
|
"clusterName": name,
|
|
})
|
|
}
|
|
}
|
|
|
|
settings := maps.Map{
|
|
"appId": app.GetString("appId"),
|
|
"appStatus": app.GetBool("isOn"),
|
|
"selectedClusterId": selectedClusterId,
|
|
"serviceAddresses": serviceAddresses,
|
|
"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
|
|
ClusterId int64
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
params.Must.Field("appId", params.AppId).Gt(0, "请选择应用")
|
|
|
|
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
|
|
}
|
|
|
|
var clusterIds []int64
|
|
if params.ClusterId > 0 {
|
|
clusterIds = []int64{params.ClusterId}
|
|
}
|
|
clusterIdsJSON, _ := json.Marshal(clusterIds)
|
|
|
|
_, err = this.RPC().HTTPDNSAppRPC().UpdateHTTPDNSApp(this.AdminContext(), &pb.UpdateHTTPDNSAppRequest{
|
|
AppDbId: params.AppId,
|
|
Name: appResp.GetApp().GetName(),
|
|
ClusterIdsJSON: clusterIdsJSON,
|
|
IsOn: params.AppStatus,
|
|
UserId: appResp.GetApp().GetUserId(),
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|