实现特殊页面配置

This commit is contained in:
GoEdgeLab
2020-09-20 08:58:48 +08:00
parent b20ab500aa
commit 6fddbc6a33
13 changed files with 400 additions and 3 deletions

View File

@@ -31,6 +31,7 @@ type RPCClient struct {
httpGzipClients []pb.HTTPGzipServiceClient
httpHeaderPolicyClients []pb.HTTPHeaderPolicyServiceClient
httpHeaderClients []pb.HTTPHeaderServiceClient
httpPageClients []pb.HTTPPageServiceClient
}
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
@@ -51,6 +52,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpGzipClients := []pb.HTTPGzipServiceClient{}
httpHeaderPolicyClients := []pb.HTTPHeaderPolicyServiceClient{}
httpHeaderClients := []pb.HTTPHeaderServiceClient{}
httpPageClients := []pb.HTTPPageServiceClient{}
conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints {
@@ -79,6 +81,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpGzipClients = append(httpGzipClients, pb.NewHTTPGzipServiceClient(conn))
httpHeaderPolicyClients = append(httpHeaderPolicyClients, pb.NewHTTPHeaderPolicyServiceClient(conn))
httpHeaderClients = append(httpHeaderClients, pb.NewHTTPHeaderServiceClient(conn))
httpPageClients = append(httpPageClients, pb.NewHTTPPageServiceClient(conn))
}
return &RPCClient{
@@ -96,6 +99,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpGzipClients: httpGzipClients,
httpHeaderPolicyClients: httpHeaderPolicyClients,
httpHeaderClients: httpHeaderClients,
httpPageClients: httpPageClients,
}, nil
}
@@ -190,6 +194,13 @@ func (this *RPCClient) HTTPHeaderPolicyRPC() pb.HTTPHeaderPolicyServiceClient {
return nil
}
func (this *RPCClient) HTTPPageRPC() pb.HTTPPageServiceClient {
if len(this.httpPageClients) > 0 {
return this.httpPageClients[rands.Int(0, len(this.httpPageClients)-1)]
}
return nil
}
func (this *RPCClient) Context(adminId int64) context.Context {
ctx := context.Background()
m := maps.Map{

View File

@@ -0,0 +1,64 @@
package pages
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
Status string
URL string `alias:"url"`
NewStatus int
Must *actions.Must
}) {
// TODO 对状态码进行更多校验
params.Must.
Field("status", params.Status).
Require("请输入响应状态码").
Field("url", params.URL).
Require("请输入要显示的URL")
createResp, err := this.RPC().HTTPPageRPC().CreateHTTPPage(this.AdminContext(), &pb.CreateHTTPPageRequest{
StatusList: []string{params.Status},
Url: params.URL,
NewStatus: types.Int32(params.NewStatus),
})
if err != nil {
this.ErrorPage(err)
return
}
pageId := createResp.PageId
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{PageId: pageId})
if err != nil {
this.ErrorPage(err)
return
}
pageConfig := &serverconfigs.HTTPPageConfig{}
err = json.Unmarshal(configResp.Config, pageConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["page"] = pageConfig
this.Success()
}

View File

@@ -1,7 +1,11 @@
package pages
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
@@ -16,7 +20,50 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// TODO
webResp, err := this.RPC().ServerRPC().FindAndInitServerWebConfig(this.AdminContext(), &pb.FindAndInitServerWebRequest{ServerId: params.ServerId})
if err != nil {
this.ErrorPage(err)
return
}
webConfig := &serverconfigs.HTTPWebConfig{}
err = json.Unmarshal(webResp.Config, webConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["pages"] = webConfig.Pages
this.Data["shutdownConfig"] = webConfig.Shutdown
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
PagesJSON string
ShutdownJSON string
Must *actions.Must
}) {
// TODO 检查配置
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebPages(this.AdminContext(), &pb.UpdateHTTPWebPagesRequest{
WebId: params.WebId,
PagesJSON: []byte(params.PagesJSON),
})
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebShutdown(this.AdminContext(), &pb.UpdateHTTPWebShutdownRequest{
WebId: params.WebId,
ShutdownJSON: []byte(params.ShutdownJSON),
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -12,7 +12,9 @@ func init() {
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/pages").
Get("", new(IndexAction)).
GetPost("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)).
GetPost("/updatePopup", new(UpdatePopupAction)).
EndAll()
})
}

View File

@@ -0,0 +1,82 @@
package pages
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type UpdatePopupAction struct {
actionutils.ParentAction
}
func (this *UpdatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *UpdatePopupAction) RunGet(params struct {
PageId int64
}) {
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{PageId: params.PageId})
if err != nil {
this.ErrorPage(err)
return
}
pageConfig := &serverconfigs.HTTPPageConfig{}
err = json.Unmarshal(configResp.Config, pageConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["pageConfig"] = pageConfig
this.Show()
}
func (this *UpdatePopupAction) RunPost(params struct {
PageId int64
Status string
URL string `alias:"url"`
NewStatus int
Must *actions.Must
}) {
params.Must.
Field("status", params.Status).
Require("请输入响应状态码").
Field("url", params.URL).
Require("请输入要显示的URL")
_, err := this.RPC().HTTPPageRPC().UpdateHTTPPage(this.AdminContext(), &pb.UpdateHTTPPageRequest{
PageId: params.PageId,
StatusList: []string{params.Status},
Url: params.URL,
NewStatus: types.Int32(params.NewStatus),
})
if err != nil {
this.ErrorPage(err)
return
}
// 返回修改后的配置
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{PageId: params.PageId})
if err != nil {
this.ErrorPage(err)
return
}
pageConfig := &serverconfigs.HTTPPageConfig{}
err = json.Unmarshal(configResp.Config, pageConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["page"] = pageConfig
this.Success()
}

View File

@@ -40,6 +40,9 @@ func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) {
secondMenuItem, _ := action.Data["secondMenuItem"]
serverId := action.ParamInt64("serverId")
if serverId == 0 {
return
}
serverIdString := strconv.FormatInt(serverId, 10)
action.Data["serverId"] = serverId