增加字符编码/HTTP Header管理

This commit is contained in:
GoEdgeLab
2020-09-16 20:29:13 +08:00
parent 12c90bc553
commit b20ab500aa
23 changed files with 827 additions and 50 deletions

View File

@@ -17,18 +17,20 @@ import (
) )
type RPCClient struct { type RPCClient struct {
apiConfig *configs.APIConfig apiConfig *configs.APIConfig
adminClients []pb.AdminServiceClient adminClients []pb.AdminServiceClient
nodeClients []pb.NodeServiceClient nodeClients []pb.NodeServiceClient
nodeGrantClients []pb.NodeGrantServiceClient nodeGrantClients []pb.NodeGrantServiceClient
nodeClusterClients []pb.NodeClusterServiceClient nodeClusterClients []pb.NodeClusterServiceClient
nodeIPAddressClients []pb.NodeIPAddressServiceClient nodeIPAddressClients []pb.NodeIPAddressServiceClient
serverClients []pb.ServerServiceClient serverClients []pb.ServerServiceClient
apiNodeClients []pb.APINodeServiceClient apiNodeClients []pb.APINodeServiceClient
originNodeClients []pb.OriginServerServiceClient originNodeClients []pb.OriginServerServiceClient
httpWebClients []pb.HTTPWebServiceClient httpWebClients []pb.HTTPWebServiceClient
reverseProxyClients []pb.ReverseProxyServiceClient reverseProxyClients []pb.ReverseProxyServiceClient
httpGzipClients []pb.HTTPGzipServiceClient httpGzipClients []pb.HTTPGzipServiceClient
httpHeaderPolicyClients []pb.HTTPHeaderPolicyServiceClient
httpHeaderClients []pb.HTTPHeaderServiceClient
} }
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) { func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
@@ -47,6 +49,8 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpWebClients := []pb.HTTPWebServiceClient{} httpWebClients := []pb.HTTPWebServiceClient{}
reverseProxyClients := []pb.ReverseProxyServiceClient{} reverseProxyClients := []pb.ReverseProxyServiceClient{}
httpGzipClients := []pb.HTTPGzipServiceClient{} httpGzipClients := []pb.HTTPGzipServiceClient{}
httpHeaderPolicyClients := []pb.HTTPHeaderPolicyServiceClient{}
httpHeaderClients := []pb.HTTPHeaderServiceClient{}
conns := []*grpc.ClientConn{} conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints { for _, endpoint := range apiConfig.RPC.Endpoints {
@@ -73,21 +77,25 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpWebClients = append(httpWebClients, pb.NewHTTPWebServiceClient(conn)) httpWebClients = append(httpWebClients, pb.NewHTTPWebServiceClient(conn))
reverseProxyClients = append(reverseProxyClients, pb.NewReverseProxyServiceClient(conn)) reverseProxyClients = append(reverseProxyClients, pb.NewReverseProxyServiceClient(conn))
httpGzipClients = append(httpGzipClients, pb.NewHTTPGzipServiceClient(conn)) httpGzipClients = append(httpGzipClients, pb.NewHTTPGzipServiceClient(conn))
httpHeaderPolicyClients = append(httpHeaderPolicyClients, pb.NewHTTPHeaderPolicyServiceClient(conn))
httpHeaderClients = append(httpHeaderClients, pb.NewHTTPHeaderServiceClient(conn))
} }
return &RPCClient{ return &RPCClient{
apiConfig: apiConfig, apiConfig: apiConfig,
adminClients: adminClients, adminClients: adminClients,
nodeClients: nodeClients, nodeClients: nodeClients,
nodeGrantClients: nodeGrantClients, nodeGrantClients: nodeGrantClients,
nodeClusterClients: nodeClusterClients, nodeClusterClients: nodeClusterClients,
nodeIPAddressClients: nodeIPAddressClients, nodeIPAddressClients: nodeIPAddressClients,
serverClients: serverClients, serverClients: serverClients,
apiNodeClients: apiNodeClients, apiNodeClients: apiNodeClients,
originNodeClients: originNodeClients, originNodeClients: originNodeClients,
httpWebClients: httpWebClients, httpWebClients: httpWebClients,
reverseProxyClients: reverseProxyClients, reverseProxyClients: reverseProxyClients,
httpGzipClients: httpGzipClients, httpGzipClients: httpGzipClients,
httpHeaderPolicyClients: httpHeaderPolicyClients,
httpHeaderClients: httpHeaderClients,
}, nil }, nil
} }
@@ -168,6 +176,20 @@ func (this *RPCClient) HTTPGzipRPC() pb.HTTPGzipServiceClient {
return nil return nil
} }
func (this *RPCClient) HTTPHeaderRPC() pb.HTTPHeaderServiceClient {
if len(this.httpHeaderClients) > 0 {
return this.httpHeaderClients[rands.Int(0, len(this.httpHeaderClients)-1)]
}
return nil
}
func (this *RPCClient) HTTPHeaderPolicyRPC() pb.HTTPHeaderPolicyServiceClient {
if len(this.httpHeaderPolicyClients) > 0 {
return this.httpHeaderPolicyClients[rands.Int(0, len(this.httpHeaderPolicyClients)-1)]
}
return nil
}
func (this *RPCClient) Context(adminId int64) context.Context { func (this *RPCClient) Context(adminId int64) context.Context {
ctx := context.Background() ctx := context.Background()
m := maps.Map{ m := maps.Map{

View File

@@ -1,7 +1,12 @@
package charset package charset
import ( import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/configutils"
"github.com/iwind/TeaGo/actions"
) )
type IndexAction struct { type IndexAction struct {
@@ -16,7 +21,41 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct { func (this *IndexAction) RunGet(params struct {
ServerId int64 ServerId int64
}) { }) {
// TODO webConfigResp, 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(webConfigResp.Config, webConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["charset"] = webConfig.Charset
this.Data["usualCharsets"] = configutils.UsualCharsets
this.Data["allCharsets"] = configutils.AllCharsets
this.Show() this.Show()
} }
func (this *IndexAction) RunPost(params struct {
WebId int64
Charset string
Must *actions.Must
}) {
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebCharset(this.AdminContext(), &pb.UpdateHTTPWebCharsetRequest{
WebId: params.WebId,
Charset: params.Charset,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

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

View File

@@ -2,9 +2,7 @@ package gzip
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/actions"
@@ -22,32 +20,26 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct { func (this *IndexAction) RunGet(params struct {
ServerId int64 ServerId int64
}) { }) {
server, _, isOk := serverutils.FindServer(&this.ParentAction, params.ServerId) webConfigResp, err := this.RPC().ServerRPC().FindAndInitServerWebConfig(this.AdminContext(), &pb.FindAndInitServerWebRequest{ServerId: params.ServerId})
if !isOk { if err != nil {
this.ErrorPage(err)
return
}
webConfig := &serverconfigs.HTTPWebConfig{}
err = json.Unmarshal(webConfigResp.Config, webConfig)
if err != nil {
this.ErrorPage(err)
return return
} }
webId := server.WebId webResp, err := this.RPC().HTTPWebRPC().FindEnabledHTTPWeb(this.AdminContext(), &pb.FindEnabledHTTPWebRequest{WebId: webConfig.Id})
if webId <= 0 {
resp, err := this.RPC().ServerRPC().InitServerWeb(this.AdminContext(), &pb.InitServerWebRequest{ServerId: params.ServerId})
if err != nil {
this.ErrorPage(err)
return
}
webId = resp.WebId
}
webResp, err := this.RPC().HTTPWebRPC().FindEnabledHTTPWeb(this.AdminContext(), &pb.FindEnabledHTTPWebRequest{WebId: webId})
if err != nil { if err != nil {
this.ErrorPage(err) this.ErrorPage(err)
return return
} }
web := webResp.Web web := webResp.Web
if web == nil {
this.ErrorPage(errors.New("web should not be nil")) this.Data["webId"] = webConfig.Id
return
}
this.Data["webId"] = web.Id
gzipId := web.GzipId gzipId := web.GzipId
gzipConfig := &serverconfigs.HTTPGzipConfig{ gzipConfig := &serverconfigs.HTTPGzipConfig{

View File

@@ -0,0 +1,57 @@
package headers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
)
type CreateDeletePopupAction struct {
actionutils.ParentAction
}
func (this *CreateDeletePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreateDeletePopupAction) RunGet(params struct {
HeaderPolicyId int64
}) {
this.Data["headerPolicyId"] = params.HeaderPolicyId
this.Show()
}
func (this *CreateDeletePopupAction) RunPost(params struct {
HeaderPolicyId int64
Name string
Must *actions.Must
}) {
policyConfigResp, err := this.RPC().HTTPHeaderPolicyRPC().FindEnabledHTTPHeaderPolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPHeaderPolicyConfigRequest{HeaderPolicyId: params.HeaderPolicyId})
if err != nil {
this.ErrorPage(err)
return
}
policyConfig := &shared.HTTPHeaderPolicy{}
err = json.Unmarshal(policyConfigResp.Config, policyConfig)
if err != nil {
this.ErrorPage(err)
return
}
deleteHeaders := policyConfig.DeletedHeaders
deleteHeaders = append(deleteHeaders, params.Name)
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicyDeletingHeaders(this.AdminContext(), &pb.UpdateHTTPHeaderPolicyDeletingHeadersRequest{
HeaderPolicyId: params.HeaderPolicyId,
HeaderNames: deleteHeaders,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,86 @@
package headers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
)
type CreateSetPopupAction struct {
actionutils.ParentAction
}
func (this *CreateSetPopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreateSetPopupAction) RunGet(params struct {
ServerId int64
HeaderPolicyId int64
}) {
this.Data["headerPolicyId"] = params.HeaderPolicyId
this.Show()
}
func (this *CreateSetPopupAction) RunPost(params struct {
HeaderPolicyId int64
Name string
Value string
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入Header名称")
configResp, err := this.RPC().HTTPHeaderPolicyRPC().FindEnabledHTTPHeaderPolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPHeaderPolicyConfigRequest{HeaderPolicyId: params.HeaderPolicyId})
if err != nil {
this.ErrorPage(err)
return
}
policyConfig := &shared.HTTPHeaderPolicy{}
err = json.Unmarshal(configResp.Config, policyConfig)
if err != nil {
this.ErrorPage(err)
return
}
// 创建Header
createHeaderResp, err := this.RPC().HTTPHeaderRPC().CreateHTTPHeader(this.AdminContext(), &pb.CreateHTTPHeaderRequest{
Name: params.Name,
Value: params.Value,
})
if err != nil {
this.ErrorPage(err)
return
}
headerId := createHeaderResp.HeaderId
// 保存
policyConfig.SetHeaders = append(policyConfig.SetHeaders, &shared.HTTPHeaderConfig{
Id: headerId,
IsOn: true,
Name: params.Name,
Value: params.Value,
Status: nil,
})
setHeadersJSON, err := json.Marshal(policyConfig.SetHeaders)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicySettingHeaders(this.AdminContext(), &pb.UpdateHTTPHeaderPolicySettingHeadersRequest{
HeaderPolicyId: params.HeaderPolicyId,
HeadersJSON: setHeadersJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,118 @@
package headers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
)
// 删除Header
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
HeaderPolicyId int64
Type string
HeaderId int64
}) {
policyConfigResp, err := this.RPC().HTTPHeaderPolicyRPC().FindEnabledHTTPHeaderPolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPHeaderPolicyConfigRequest{
HeaderPolicyId: params.HeaderPolicyId,
})
if err != nil {
this.ErrorPage(err)
return
}
policyConfig := &shared.HTTPHeaderPolicy{}
err = json.Unmarshal(policyConfigResp.Config, policyConfig)
if err != nil {
this.ErrorPage(err)
return
}
switch params.Type {
case "addHeader":
result := []*shared.HTTPHeaderConfig{}
for _, h := range policyConfig.AddHeaders {
if h.Id != params.HeaderId {
result = append(result, h)
}
}
resultJSON, err := json.Marshal(result)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicyAddingHeaders(this.AdminContext(), &pb.UpdateHTTPHeaderPolicyAddingHeadersRequest{
HeaderPolicyId: params.HeaderPolicyId,
HeadersJSON: resultJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
case "setHeader":
result := []*shared.HTTPHeaderConfig{}
for _, h := range policyConfig.SetHeaders {
if h.Id != params.HeaderId {
result = append(result, h)
}
}
resultJSON, err := json.Marshal(result)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicySettingHeaders(this.AdminContext(), &pb.UpdateHTTPHeaderPolicySettingHeadersRequest{
HeaderPolicyId: params.HeaderPolicyId,
HeadersJSON: resultJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
case "replace":
result := []*shared.HTTPHeaderConfig{}
for _, h := range policyConfig.ReplaceHeaders {
if h.Id != params.HeaderId {
result = append(result, h)
}
}
resultJSON, err := json.Marshal(result)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicyReplacingHeaders(this.AdminContext(), &pb.UpdateHTTPHeaderPolicyReplacingHeadersRequest{
HeaderPolicyId: params.HeaderPolicyId,
HeadersJSON: resultJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
case "addTrailer":
result := []*shared.HTTPHeaderConfig{}
for _, h := range policyConfig.AddTrailers {
if h.Id != params.HeaderId {
result = append(result, h)
}
}
resultJSON, err := json.Marshal(result)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicyAddingTrailers(this.AdminContext(), &pb.UpdateHTTPHeaderPolicyAddingTrailersRequest{
HeaderPolicyId: params.HeaderPolicyId,
HeadersJSON: resultJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
}
this.Success()
}

View File

@@ -0,0 +1,48 @@
package headers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
)
type DeleteDeletingHeaderAction struct {
actionutils.ParentAction
}
func (this *DeleteDeletingHeaderAction) RunPost(params struct {
HeaderPolicyId int64
HeaderName string
}) {
policyConfigResp, err := this.RPC().HTTPHeaderPolicyRPC().FindEnabledHTTPHeaderPolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPHeaderPolicyConfigRequest{HeaderPolicyId: params.HeaderPolicyId})
if err != nil {
this.ErrorPage(err)
return
}
policyConfigJSON := policyConfigResp.Config
policyConfig := &shared.HTTPHeaderPolicy{}
err = json.Unmarshal(policyConfigJSON, policyConfig)
if err != nil {
this.ErrorPage(err)
return
}
headerNames := []string{}
for _, h := range policyConfig.DeletedHeaders {
if h == params.HeaderName {
continue
}
headerNames = append(headerNames, h)
}
_, err = this.RPC().HTTPHeaderPolicyRPC().UpdateHTTPHeaderPolicyDeletingHeaders(this.AdminContext(), &pb.UpdateHTTPHeaderPolicyDeletingHeadersRequest{
HeaderPolicyId: params.HeaderPolicyId,
HeaderNames: headerNames,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -1,7 +1,11 @@
package headers package headers
import ( import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
) )
type IndexAction struct { type IndexAction struct {
@@ -16,7 +20,73 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct { func (this *IndexAction) RunGet(params struct {
ServerId int64 ServerId int64
}) { }) {
// TODO webConfigResp, 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(webConfigResp.Config, webConfig)
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 {
webConfigResp, err := this.RPC().ServerRPC().FindAndInitServerWebConfig(this.AdminContext(), &pb.FindAndInitServerWebRequest{ServerId: params.ServerId})
if err != nil {
this.ErrorPage(err)
return
}
err = json.Unmarshal(webConfigResp.Config, webConfig)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["requestHeaderPolicy"] = webConfig.RequestHeaders
this.Data["responseHeaderPolicy"] = webConfig.ResponseHeaders
this.Show() this.Show()
} }

View File

@@ -13,6 +13,11 @@ func init() {
Helper(serverutils.NewServerHelper()). Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/headers"). Prefix("/servers/server/settings/headers").
Get("", new(IndexAction)). Get("", new(IndexAction)).
GetPost("/createSetPopup", new(CreateSetPopupAction)).
GetPost("/updateSetPopup", new(UpdateSetPopupAction)).
GetPost("/createDeletePopup", new(CreateDeletePopupAction)).
Post("/deleteDeletingHeader", new(DeleteDeletingHeaderAction)).
Post("/delete", new(DeleteAction)).
EndAll() EndAll()
}) })
} }

View File

@@ -0,0 +1,63 @@
package headers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
)
type UpdateSetPopupAction struct {
actionutils.ParentAction
}
func (this *UpdateSetPopupAction) Init() {
this.Nav("", "", "")
}
func (this *UpdateSetPopupAction) RunGet(params struct {
HeaderPolicyId int64
HeaderId int64
}) {
this.Data["headerPolicyId"] = params.HeaderPolicyId
this.Data["headerId"] = params.HeaderId
headerResp, err := this.RPC().HTTPHeaderRPC().FindEnabledHTTPHeaderConfig(this.AdminContext(), &pb.FindEnabledHTTPHeaderConfigRequest{HeaderId: params.HeaderId})
if err != nil {
this.ErrorPage(err)
return
}
headerConfig := &shared.HTTPHeaderConfig{}
err = json.Unmarshal(headerResp.Config, headerConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["headerConfig"] = headerConfig
this.Show()
}
func (this *UpdateSetPopupAction) RunPost(params struct {
HeaderId int64
Name string
Value string
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入Header名称")
_, err := this.RPC().HTTPHeaderRPC().UpdateHTTPHeader(this.AdminContext(), &pb.UpdateHTTPHeaderRequest{
HeaderId: params.HeaderId,
Name: params.Name,
Value: params.Value,
})
if err != nil {
this.ErrorPage(err)
}
this.Success()
}

View File

@@ -201,7 +201,7 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
}) })
menuItems = append(menuItems, maps.Map{ menuItems = append(menuItems, maps.Map{
"name": "字符", "name": "字符编码",
"url": "/servers/server/settings/charset?serverId=" + serverIdString, "url": "/servers/server/settings/charset?serverId=" + serverIdString,
"isActive": secondMenuItem == "charset", "isActive": secondMenuItem == "charset",
}) })

View File

@@ -0,0 +1,28 @@
Vue.component("charsets-box", {
props: ["v-usual-charsets", "v-all-charsets", "v-charset"],
data: function () {
let charset = this.vCharset
if (charset == null) {
charset = ""
}
return {
charset: charset
}
},
template: `<div>
<table class="ui table definition selectable">
<tr>
<td class="title">选择字符编码</td>
<td><select class="ui dropdown auto-width" name="charset" v-model="charset">
<option value="">[未选择]</option>
<optgroup label="常用字符编码"></optgroup>
<option v-for="charset in vUsualCharsets" :value="charset.charset">{{charset.charset}}{{charset.name}}</option>
<optgroup label="全部字符编码"></optgroup>
<option v-for="charset in vAllCharsets" :value="charset.charset">{{charset.charset}}{{charset.name}}</option>
</select>
</td>
</tr>
</table>
<div class="margin"></div>
</div>`
})

View File

@@ -0,0 +1,173 @@
Vue.component("header-policy-box", {
props: ["v-request-header-policy", "v-response-header-policy", "v-params"],
data: function () {
let type = "request"
let hash = window.location.hash
if (hash == "#response") {
type = "response"
}
// 请求相关
let requestSettingHeaders = []
let requestDeletingHeaders = []
let requestPolicy = this.vRequestHeaderPolicy
if (requestPolicy != null) {
if (requestPolicy.setHeaders != null) {
requestSettingHeaders = requestPolicy.setHeaders
}
if (requestPolicy.deleteHeaders != null) {
requestDeletingHeaders = requestPolicy.deleteHeaders
}
}
// 响应相关
let responseSettingHeaders = []
let responseDeletingHeaders = []
let responsePolicy = this.vResponseHeaderPolicy
if (responsePolicy != null) {
if (responsePolicy.setHeaders != null) {
responseSettingHeaders = responsePolicy.setHeaders
}
if (responsePolicy.deleteHeaders != null) {
responseDeletingHeaders = responsePolicy.deleteHeaders
}
}
return {
type: type,
requestSettingHeaders: requestSettingHeaders,
requestDeletingHeaders: requestDeletingHeaders,
responseSettingHeaders: responseSettingHeaders,
responseDeletingHeaders: responseDeletingHeaders
}
},
methods: {
selectType: function (type) {
this.type = type
window.location.hash = "#" + type
},
addSettingHeader: function (policyId) {
teaweb.popup("/servers/server/settings/headers/createSetPopup?" + this.vParams + "&headerPolicyId=" + policyId, {
callback: function () {
window.location.reload()
}
})
},
addDeletingHeader: function (policyId, type) {
teaweb.popup("/servers/server/settings/headers/createDeletePopup?" + this.vParams + "&headerPolicyId=" + policyId + "&type=" + type, {
callback: function () {
window.location.reload()
}
})
},
updateSettingPopup: function (policyId, headerId) {
teaweb.popup("/servers/server/settings/headers/updateSetPopup?" + this.vParams + "&headerPolicyId=" + policyId + "&headerId=" + headerId, {
callback: function () {
window.location.reload()
}
})
},
deleteDeletingHeader: function (policyId, headerName) {
teaweb.confirm("确定要删除'" + headerName + "'吗?", function () {
Tea.action("/servers/server/settings/headers/deleteDeletingHeader")
.params({
headerPolicyId: policyId,
headerName: headerName
})
.post()
.refresh()
})
},
deleteHeader: function (policyId, type, headerId) {
teaweb.confirm("确定要删除此Header吗", function () {
this.$post("/servers/server/settings/headers/delete")
.params({
headerPolicyId: policyId,
type: type,
headerId: headerId
})
.refresh()
}
)
}
},
template: `<div>
<first-menu>
<a class="item" :class="{active:type == 'request'}" @click.prevent="selectType('request')">请求Header</a>
<a class="item" :class="{active:type == 'response'}" @click.prevent="selectType('response')">响应Header</a>
</first-menu>
<div class="margin"></div>
<!-- 请求 -->
<div v-if="type == 'request'">
<h3>设置Header <a href="" @click.prevent="addSettingHeader(vRequestHeaderPolicy.id)">[添加新Header]</a></h3>
<p class="comment" v-if="requestSettingHeaders.length == 0">暂时还没有Header。</p>
<table class="ui table selectable" v-if="requestSettingHeaders.length > 0">
<thead>
<tr>
<th>名称</th>
<th>值</th>
<th class="two op">操作</th>
</tr>
</thead>
<tr v-for="header in requestSettingHeaders">
<td class="five wide">{{header.name}}</td>
<td>{{header.value}}</td>
<td><a href="" @click.prevent="updateSettingPopup(vRequestHeaderPolicy.id, header.id)">修改</a> &nbsp; <a href="" @click.prevent="deleteHeader(vRequestHeaderPolicy.id, 'setHeader', header.id)">删除</a> </td>
</tr>
</table>
<h3>删除Header</h3>
<p class="comment">这里可以设置需要从请求中删除的Header。</p>
<table class="ui table definition selectable">
<td class="title">需要删除的Header</td>
<td>
<div v-if="requestDeletingHeaders.length > 0">
<div class="ui label small" v-for="headerName in requestDeletingHeaders">{{headerName}} <a href=""><i class="icon remove" title="删除" @click.prevent="deleteDeletingHeader(vRequestHeaderPolicy.id, headerName)"></i></a> </div>
<div class="ui divider" ></div>
</div>
<button class="ui button small" type="button" @click.prevent="addDeletingHeader(vRequestHeaderPolicy.id, 'request')">+</button>
</td>
</table>
</div>
<!-- 响应 -->
<div v-if="type == 'response'">
<h3>设置Header <a href="" @click.prevent="addSettingHeader(vResponseHeaderPolicy.id)">[添加新Header]</a></h3>
<p class="comment" v-if="responseSettingHeaders.length == 0">暂时还没有Header。</p>
<table class="ui table selectable" v-if="responseSettingHeaders.length > 0">
<thead>
<tr>
<th>名称</th>
<th>值</th>
<th class="two op">操作</th>
</tr>
</thead>
<tr v-for="header in responseSettingHeaders">
<td class="five wide">{{header.name}}</td>
<td>{{header.value}}</td>
<td><a href="" @click.prevent="updateSettingPopup(vResponseHeaderPolicy.id, header.id)">修改</a> &nbsp; <a href="" @click.prevent="deleteHeader(vResponseHeaderPolicy.id, 'setHeader', header.id)">删除</a> </td>
</tr>
</table>
<h3>删除Header</h3>
<p class="comment">这里可以设置需要从响应中删除的Header。</p>
<table class="ui table definition selectable">
<td class="title">需要删除的Header</td>
<td>
<div v-if="responseDeletingHeaders.length > 0">
<div class="ui label small" v-for="headerName in responseDeletingHeaders">{{headerName}} <a href=""><i class="icon remove" title="删除" @click.prevent="deleteDeletingHeader(vResponseHeaderPolicy.id, headerName)"></i></a> </div>
<div class="ui divider" ></div>
</div>
<button class="ui button small" type="button" @click.prevent="addDeletingHeader(vResponseHeaderPolicy.id, 'response')">+</button>
</td>
</table>
</div>
</div>`
})

View File

@@ -3,5 +3,9 @@
{$template "/left_menu"} {$template "/left_menu"}
<div class="right-box"> <div class="right-box">
<p class="ui message">此功能暂未开放,敬请期待。</p> <form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="webId" :value="webId"/>
<charsets-box :v-usual-charsets="usualCharsets" :v-all-charsets="allCharsets" :v-charset="charset"></charsets-box>
<submit-btn></submit-btn>
</form>
</div> </div>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
})

View File

@@ -0,0 +1,15 @@
{$layout "layout_popup"}
<h3>添加需要删除的Header</h3>
<form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="headerPolicyId" :value="headerPolicyId"/>
<table class="ui table definition selectable">
<tr>
<td class="title">名称<em>Name</em></td>
<td>
<input type="text" name="name" maxlength="100" ref="focus"/>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyPopup
})

View File

@@ -0,0 +1,22 @@
{$layout "layout_popup"}
<h3>设置Header</h3>
<form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="serverId" :value="serverId"/>
<input type="hidden" name="headerPolicyId" :value="headerPolicyId"/>
<table class="ui table definition selectable">
<tr>
<td class="title">名称<em>Name</em></td>
<td>
<input type="text" name="name" value="" maxlength="200" placeholder="类似于Server、Content-Type之类" ref="focus"/>
</td>
</tr>
<tr>
<td><em>Value</em></td>
<td>
<input type="text" name="value" maxlength="500"/>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyPopup
})

View File

@@ -3,5 +3,5 @@
{$template "/left_menu"} {$template "/left_menu"}
<div class="right-box"> <div class="right-box">
<p class="ui message">此功能暂未开放,敬请期待。</p> <header-policy-box :v-request-header-policy="requestHeaderPolicy" :v-response-header-policy="responseHeaderPolicy" :v-params="'serverId=' + serverId"></header-policy-box>
</div> </div>

View File

@@ -0,0 +1,23 @@
{$layout "layout_popup"}
<h3>修改Header</h3>
<form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="serverId" :value="serverId"/>
<input type="hidden" name="headerPolicyId" :value="headerPolicyId"/>
<input type="hidden" name="headerId" :value="headerId"/>
<table class="ui table definition selectable">
<tr>
<td class="title">名称<em>Name</em></td>
<td>
<input type="text" name="name" value="" v-model="headerConfig.name" maxlength="200" placeholder="类似于Server、Content-Type之类" ref="focus"/>
</td>
</tr>
<tr>
<td><em>Value</em></td>
<td>
<input type="text" name="value" v-model="headerConfig.value" maxlength="500"/>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyPopup
})