2020-08-21 21:09:42 +08:00
|
|
|
package headers
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-23 18:43:38 +08:00
|
|
|
"encoding/json"
|
2020-08-21 21:09:42 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2021-01-03 20:25:15 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
2020-09-16 20:29:13 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-09-23 18:43:38 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
2020-08-21 21:09:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IndexAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) Init() {
|
|
|
|
|
this.Nav("", "setting", "index")
|
|
|
|
|
this.SecondMenu("header")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
|
|
|
ServerId int64
|
|
|
|
|
}) {
|
2021-01-03 20:25:15 +08:00
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
2020-09-16 20:29:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-23 18:43:38 +08:00
|
|
|
webId := webConfig.Id
|
2020-09-16 20:29:13 +08:00
|
|
|
|
|
|
|
|
isChanged := false
|
2020-09-23 18:43:38 +08:00
|
|
|
if webConfig.RequestHeaderPolicy == nil {
|
2020-09-16 20:29:13 +08:00
|
|
|
createHeaderPolicyResp, err := this.RPC().HTTPHeaderPolicyRPC().CreateHTTPHeaderPolicy(this.AdminContext(), &pb.CreateHTTPHeaderPolicyRequest{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
headerPolicyId := createHeaderPolicyResp.HeaderPolicyId
|
2020-09-23 18:43:38 +08:00
|
|
|
ref := &shared.HTTPHeaderPolicyRef{
|
|
|
|
|
IsPrior: false,
|
|
|
|
|
IsOn: true,
|
2020-09-16 20:29:13 +08:00
|
|
|
HeaderPolicyId: headerPolicyId,
|
2020-09-23 18:43:38 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2020-09-16 20:29:13 +08:00
|
|
|
})
|
2020-12-23 09:52:31 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-16 20:29:13 +08:00
|
|
|
isChanged = true
|
|
|
|
|
}
|
2020-09-23 18:43:38 +08:00
|
|
|
if webConfig.ResponseHeaderPolicy == nil {
|
2020-09-16 20:29:13 +08:00
|
|
|
createHeaderPolicyResp, err := this.RPC().HTTPHeaderPolicyRPC().CreateHTTPHeaderPolicy(this.AdminContext(), &pb.CreateHTTPHeaderPolicyRequest{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
headerPolicyId := createHeaderPolicyResp.HeaderPolicyId
|
2020-09-23 18:43:38 +08:00
|
|
|
ref := &shared.HTTPHeaderPolicyRef{
|
|
|
|
|
IsPrior: false,
|
|
|
|
|
IsOn: true,
|
2020-09-16 20:29:13 +08:00
|
|
|
HeaderPolicyId: headerPolicyId,
|
2020-09-23 18:43:38 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2020-09-16 20:29:13 +08:00
|
|
|
})
|
2020-12-23 09:52:31 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-16 20:29:13 +08:00
|
|
|
isChanged = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重新获取配置
|
|
|
|
|
if isChanged {
|
2021-01-03 20:25:15 +08:00
|
|
|
webConfig, err = dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
2020-09-16 20:29:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 18:43:38 +08:00
|
|
|
this.Data["requestHeaderRef"] = webConfig.RequestHeaderPolicyRef
|
|
|
|
|
this.Data["requestHeaderPolicy"] = webConfig.RequestHeaderPolicy
|
|
|
|
|
this.Data["responseHeaderRef"] = webConfig.ResponseHeaderPolicyRef
|
|
|
|
|
this.Data["responseHeaderPolicy"] = webConfig.ResponseHeaderPolicy
|
2020-08-21 21:09:42 +08:00
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|