2020-08-21 21:09:42 +08:00
|
|
|
package headers
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-16 20:29:13 +08:00
|
|
|
"errors"
|
2020-08-21 21:09:42 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2020-09-21 19:51:50 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
|
2020-09-16 20:29:13 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
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
|
|
|
|
|
}) {
|
2020-09-21 19:51:50 +08:00
|
|
|
webConfig, err := webutils.FindWebConfigWithServerId(this.Parent(), params.ServerId)
|
2020-09-16 20:29:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化Header
|
|
|
|
|
webResp, err := this.RPC().HTTPWebRPC().FindEnabledHTTPWeb(this.AdminContext(), &pb.FindEnabledHTTPWebRequest{WebId: webConfig.Id})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
web := webResp.Web
|
|
|
|
|
if web == nil {
|
|
|
|
|
this.ErrorPage(errors.New("web should not be nil"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
isChanged := false
|
|
|
|
|
if web.RequestHeaderPolicyId <= 0 {
|
|
|
|
|
createHeaderPolicyResp, err := this.RPC().HTTPHeaderPolicyRPC().CreateHTTPHeaderPolicy(this.AdminContext(), &pb.CreateHTTPHeaderPolicyRequest{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
headerPolicyId := createHeaderPolicyResp.HeaderPolicyId
|
|
|
|
|
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRequestHeaderPolicy(this.AdminContext(), &pb.UpdateHTTPWebRequestHeaderPolicyRequest{
|
|
|
|
|
WebId: web.Id,
|
|
|
|
|
HeaderPolicyId: headerPolicyId,
|
|
|
|
|
})
|
|
|
|
|
isChanged = true
|
|
|
|
|
}
|
|
|
|
|
if web.ResponseHeaderPolicyId <= 0 {
|
|
|
|
|
createHeaderPolicyResp, err := this.RPC().HTTPHeaderPolicyRPC().CreateHTTPHeaderPolicy(this.AdminContext(), &pb.CreateHTTPHeaderPolicyRequest{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
headerPolicyId := createHeaderPolicyResp.HeaderPolicyId
|
|
|
|
|
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebResponseHeaderPolicy(this.AdminContext(), &pb.UpdateHTTPWebResponseHeaderPolicyRequest{
|
|
|
|
|
WebId: web.Id,
|
|
|
|
|
HeaderPolicyId: headerPolicyId,
|
|
|
|
|
})
|
|
|
|
|
isChanged = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重新获取配置
|
|
|
|
|
if isChanged {
|
2020-09-21 19:51:50 +08:00
|
|
|
webConfig, err = webutils.FindWebConfigWithServerId(this.Parent(), params.ServerId)
|
2020-09-16 20:29:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Data["requestHeaderPolicy"] = webConfig.RequestHeaders
|
|
|
|
|
this.Data["responseHeaderPolicy"] = webConfig.ResponseHeaders
|
2020-08-21 21:09:42 +08:00
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|