mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-03 20:40:26 +08:00
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package headers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
)
|
|
|
|
type IndexAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *IndexAction) Init() {
|
|
this.Nav("", "setting", "index")
|
|
this.SecondMenu("header")
|
|
}
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
ServerId int64
|
|
}) {
|
|
webConfig, err := webutils.FindWebConfigWithServerId(this.Parent(), params.ServerId)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
webId := webConfig.Id
|
|
|
|
isChanged := false
|
|
if webConfig.RequestHeaderPolicy == nil {
|
|
createHeaderPolicyResp, err := this.RPC().HTTPHeaderPolicyRPC().CreateHTTPHeaderPolicy(this.AdminContext(), &pb.CreateHTTPHeaderPolicyRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
headerPolicyId := createHeaderPolicyResp.HeaderPolicyId
|
|
ref := &shared.HTTPHeaderPolicyRef{
|
|
IsPrior: false,
|
|
IsOn: true,
|
|
HeaderPolicyId: headerPolicyId,
|
|
}
|
|
refJSON, err := json.Marshal(ref)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRequestHeader(this.AdminContext(), &pb.UpdateHTTPWebRequestHeaderRequest{
|
|
WebId: webId,
|
|
HeaderJSON: refJSON,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
isChanged = true
|
|
}
|
|
if webConfig.ResponseHeaderPolicy == nil {
|
|
createHeaderPolicyResp, err := this.RPC().HTTPHeaderPolicyRPC().CreateHTTPHeaderPolicy(this.AdminContext(), &pb.CreateHTTPHeaderPolicyRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
headerPolicyId := createHeaderPolicyResp.HeaderPolicyId
|
|
ref := &shared.HTTPHeaderPolicyRef{
|
|
IsPrior: false,
|
|
IsOn: true,
|
|
HeaderPolicyId: headerPolicyId,
|
|
}
|
|
refJSON, err := json.Marshal(ref)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebResponseHeader(this.AdminContext(), &pb.UpdateHTTPWebResponseHeaderRequest{
|
|
WebId: webId,
|
|
HeaderJSON: refJSON,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
isChanged = true
|
|
}
|
|
|
|
// 重新获取配置
|
|
if isChanged {
|
|
webConfig, err = webutils.FindWebConfigWithServerId(this.Parent(), params.ServerId)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
this.Data["requestHeaderRef"] = webConfig.RequestHeaderPolicyRef
|
|
this.Data["requestHeaderPolicy"] = webConfig.RequestHeaderPolicy
|
|
this.Data["responseHeaderRef"] = webConfig.ResponseHeaderPolicyRef
|
|
this.Data["responseHeaderPolicy"] = webConfig.ResponseHeaderPolicy
|
|
|
|
this.Show()
|
|
}
|