实现修改缓存配置

This commit is contained in:
GoEdgeLab
2020-09-20 16:28:16 +08:00
parent b907619117
commit 679d60d654
9 changed files with 144 additions and 20 deletions

View File

@@ -33,6 +33,7 @@ type RPCClient struct {
httpHeaderClients []pb.HTTPHeaderServiceClient
httpPageClients []pb.HTTPPageServiceClient
httpAccessLogPolicyClients []pb.HTTPAccessLogPolicyServiceClient
httpCachePolicyClients []pb.HTTPCachePolicyServiceClient
}
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
@@ -55,6 +56,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpHeaderClients := []pb.HTTPHeaderServiceClient{}
httpPageClients := []pb.HTTPPageServiceClient{}
httpAccessLogPolicyClients := []pb.HTTPAccessLogPolicyServiceClient{}
httpCachePolicyClients := []pb.HTTPCachePolicyServiceClient{}
conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints {
@@ -85,6 +87,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpHeaderClients = append(httpHeaderClients, pb.NewHTTPHeaderServiceClient(conn))
httpPageClients = append(httpPageClients, pb.NewHTTPPageServiceClient(conn))
httpAccessLogPolicyClients = append(httpAccessLogPolicyClients, pb.NewHTTPAccessLogPolicyServiceClient(conn))
httpCachePolicyClients = append(httpCachePolicyClients, pb.NewHTTPCachePolicyServiceClient(conn))
}
return &RPCClient{
@@ -104,6 +107,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpHeaderClients: httpHeaderClients,
httpPageClients: httpPageClients,
httpAccessLogPolicyClients: httpAccessLogPolicyClients,
httpCachePolicyClients: httpCachePolicyClients,
}, nil
}
@@ -212,6 +216,13 @@ func (this *RPCClient) HTTPAccessLogPolicyRPC() pb.HTTPAccessLogPolicyServiceCli
return nil
}
func (this *RPCClient) HTTPCachePolicyRPC() pb.HTTPCachePolicyServiceClient {
if len(this.httpCachePolicyClients) > 0 {
return this.httpCachePolicyClients[rands.Int(0, len(this.httpCachePolicyClients)-1)]
}
return nil
}
func (this *RPCClient) Context(adminId int64) context.Context {
ctx := context.Background()
m := maps.Map{

View File

@@ -34,7 +34,7 @@ func (this *IndexAction) RunGet(params struct {
return
}
this.Data["webId"] = webConfig.Id
this.Data["accessLogConfig"] = webConfig.AccessLog
this.Data["accessLogConfig"] = webConfig.AccessLogRef
// 可选的缓存策略
policiesResp, err := this.RPC().HTTPAccessLogPolicyRPC().FindAllEnabledHTTPAccessLogPolicies(this.AdminContext(), &pb.FindAllEnabledHTTPAccessLogPoliciesRequest{})

View File

@@ -1,7 +1,12 @@
package cache
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/maps"
)
type IndexAction struct {
@@ -16,7 +21,56 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct {
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["cacheConfig"] = webConfig.CacheRef
// 所有缓存策略
cachePoliciesResp, err := this.RPC().HTTPCachePolicyRPC().FindAllEnabledHTTPCachePolicies(this.AdminContext(), &pb.FindAllEnabledHTTPCachePoliciesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
policyMaps := []maps.Map{}
for _, policy := range cachePoliciesResp.CachePolicies {
policyMaps = append(policyMaps, maps.Map{
"id": policy.Id,
"name": policy.Name,
"isOn": policy.IsOn,
})
}
this.Data["policies"] = policyMaps
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
CacheJSON []byte
Must *actions.Must
}) {
// TODO 校验配置
_, err := this.RPC().HTTPWebRPC().UpdateHTTPCache(this.AdminContext(), &pb.UpdateHTTPCacheRequest{
WebId: params.WebId,
CacheJSON: params.CacheJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

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

View File

@@ -6,6 +6,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type IndexAction struct {
@@ -32,16 +33,12 @@ func (this *IndexAction) RunGet(params struct {
return
}
webResp, err := this.RPC().HTTPWebRPC().FindEnabledHTTPWeb(this.AdminContext(), &pb.FindEnabledHTTPWebRequest{WebId: webConfig.Id})
if err != nil {
this.ErrorPage(err)
return
}
web := webResp.Web
this.Data["webId"] = webConfig.Id
gzipId := web.GzipId
gzipId := int64(0)
if webConfig.GzipRef != nil {
gzipId = webConfig.GzipRef.GzipId
}
gzipConfig := &serverconfigs.HTTPGzipConfig{
Id: 0,
IsOn: true,
@@ -95,11 +92,10 @@ func (this *IndexAction) RunPost(params struct {
}
}
if params.GzipId > 0 {
_, err := this.RPC().HTTPGzipRPC().UpdateHTTPGzip(this.AdminContext(), &pb.UpdateHTTPGzipRequest{
GzipId: params.GzipId,
Level: int32(params.Level),
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
})
@@ -109,9 +105,9 @@ func (this *IndexAction) RunPost(params struct {
}
} else {
resp, err := this.RPC().HTTPGzipRPC().CreateHTTPGzip(this.AdminContext(), &pb.CreateHTTPGzipRequest{
Level: 0,
MinLength: nil,
MaxLength: nil,
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
})
if err != nil {
this.ErrorPage(err)
@@ -119,9 +115,19 @@ func (this *IndexAction) RunPost(params struct {
}
gzipId := resp.GzipId
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{
WebId: params.WebId,
gzipRef := &serverconfigs.HTTPGzipRef{
IsOn: true,
GzipId: gzipId,
}
gzipRefJSON, err := json.Marshal(gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{
WebId: params.WebId,
GzipJSON: gzipRefJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -33,7 +33,7 @@ func (this *IndexAction) RunGet(params struct {
}
this.Data["webId"] = webConfig.Id
this.Data["statConfig"] = webConfig.Stat
this.Data["statConfig"] = webConfig.StatRef
this.Show()
}