实现服务的缓存策略设置

This commit is contained in:
GoEdgeLab
2020-10-04 20:38:27 +08:00
parent db21a86825
commit 0bbd178b29
25 changed files with 502 additions and 133 deletions

View File

@@ -113,5 +113,13 @@ func (this *ParentAction) RPC() *rpc.RPCClient {
// 获取Context
func (this *ParentAction) AdminContext() context.Context {
if this.rpcClient == nil {
rpcClient, err := rpc.SharedRPC()
if err != nil {
logs.Fatal(err)
return nil
}
this.rpcClient = rpcClient
}
return this.rpcClient.Context(this.AdminId())
}

View File

@@ -17,7 +17,7 @@ func (this *CreatePopupAction) Init() {
}
func (this *CreatePopupAction) RunGet(params struct{}) {
this.Data["types"] = serverconfigs.AllCachePolicyTypes
this.Data["types"] = serverconfigs.AllCachePolicyStorageTypes
this.Show()
}
@@ -44,15 +44,15 @@ func (this *CreatePopupAction) RunPost(params struct {
// 校验选项
var options interface{}
switch params.Type {
case serverconfigs.CachePolicyTypeFile:
case serverconfigs.CachePolicyStorageFile:
params.Must.
Field("fileDir", params.FileDir).
Require("请输入缓存目录")
options = &serverconfigs.HTTPFileCacheConfig{
options = &serverconfigs.HTTPFileCacheStorage{
Dir: params.FileDir,
}
case serverconfigs.CachePolicyTypeMemory:
options = &serverconfigs.HTTPMemoryCacheConfig{
case serverconfigs.CachePolicyStorageMemory:
options = &serverconfigs.HTTPMemoryCacheStorage{
}
default:
this.Fail("请选择正确的缓存类型")

View File

@@ -53,7 +53,7 @@ func (this *IndexAction) RunGet(params struct{}) {
countServers := countServersResp.Count
infos = append(infos, maps.Map{
"typeName": serverconfigs.FindCachePolicyTypeName(cachePolicy.Type),
"typeName": serverconfigs.FindCachePolicyStorageName(cachePolicy.Type),
"countServers": countServers,
})
}

View File

@@ -24,7 +24,7 @@ func (this *PolicyAction) RunGet(params struct {
}
this.Data["cachePolicy"] = cachePolicy
this.Data["typeName"] = serverconfigs.FindCachePolicyTypeName(cachePolicy.Type)
this.Data["typeName"] = serverconfigs.FindCachePolicyStorageName(cachePolicy.Type)
this.Show()
}

View File

@@ -39,7 +39,7 @@ func (this *UpdateAction) RunGet(params struct {
this.Data["cachePolicy"] = cachePolicy
// 其他选项
this.Data["types"] = serverconfigs.AllCachePolicyTypes
this.Data["types"] = serverconfigs.AllCachePolicyStorageTypes
this.Show()
}
@@ -69,15 +69,15 @@ func (this *UpdateAction) RunPost(params struct {
// 校验选项
var options interface{}
switch params.Type {
case serverconfigs.CachePolicyTypeFile:
case serverconfigs.CachePolicyStorageFile:
params.Must.
Field("fileDir", params.FileDir).
Require("请输入缓存目录")
options = &serverconfigs.HTTPFileCacheConfig{
options = &serverconfigs.HTTPFileCacheStorage{
Dir: params.FileDir,
}
case serverconfigs.CachePolicyTypeMemory:
options = &serverconfigs.HTTPMemoryCacheConfig{
case serverconfigs.CachePolicyStorageMemory:
options = &serverconfigs.HTTPMemoryCacheStorage{
}
default:
this.Fail("请选择正确的缓存类型")

View File

@@ -0,0 +1,81 @@
package cache
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/models"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
// 缓存策略列表
cachePoliciesResp, err := this.RPC().HTTPCachePolicyRPC().FindAllEnabledHTTPCachePolicies(this.AdminContext(), &pb.FindAllEnabledHTTPCachePoliciesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
cachePolicyMaps := []maps.Map{}
for _, cachePolicy := range cachePoliciesResp.CachePolicies {
cachePolicyMaps = append(cachePolicyMaps, maps.Map{
"id": cachePolicy.Id,
"name": cachePolicy.Name,
})
}
this.Data["cachePolicies"] = cachePolicyMaps
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
CachePolicyId int64
CacheRefJSON []byte
Must *actions.Must
}) {
if params.CachePolicyId <= 0 {
this.Fail("请选择要使用的缓存策略")
}
cachePolicy, err := models.SharedHTTPCachePolicyDAO.FindEnabledCachePolicyConfig(this.AdminContext(), params.CachePolicyId)
if err != nil {
this.ErrorPage(err)
return
}
if cachePolicy == nil {
this.Fail("找不到你要使用的缓存策略")
}
cacheRef := &serverconfigs.HTTPCacheRef{}
err = json.Unmarshal(params.CacheRefJSON, cacheRef)
if err != nil {
this.ErrorPage(err)
return
}
if len(cacheRef.Key) == 0 {
this.Fail("请输入缓存Key")
}
cacheRef.CachePolicyId = cachePolicy.Id
cacheRef.CachePolicy = cachePolicy
err = cacheRef.Init()
if err != nil {
this.ErrorPage(err)
return
}
this.Data["cacheRef"] = cacheRef
this.Success()
}

View File

@@ -1,11 +1,12 @@
package cache
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"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
@@ -27,23 +28,7 @@ func (this *IndexAction) RunGet(params struct {
}
this.Data["webId"] = webConfig.Id
this.Data["cacheConfig"] = webConfig.CacheRefs
// 所有缓存策略
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.Data["cacheConfig"] = webConfig.Cache
this.Show()
}
@@ -55,10 +40,27 @@ func (this *IndexAction) RunPost(params struct {
Must *actions.Must
}) {
// TODO 校验配置
cacheConfig := &serverconfigs.HTTPCacheConfig{}
err := json.Unmarshal(params.CacheJSON, cacheConfig)
if err != nil {
this.ErrorPage(err)
return
}
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebCache(this.AdminContext(), &pb.UpdateHTTPWebCacheRequest{
// 去除不必要的部分
for _, cacheRef := range cacheConfig.CacheRefs {
cacheRef.CachePolicy = nil
}
cacheJSON, err := json.Marshal(cacheConfig)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebCache(this.AdminContext(), &pb.UpdateHTTPWebCacheRequest{
WebId: params.WebId,
CacheJSON: params.CacheJSON,
CacheJSON: cacheJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -13,6 +13,7 @@ func init() {
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/cache").
GetPost("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)).
EndAll()
})
}

View File

@@ -1,11 +1,12 @@
package cache
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"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
@@ -25,23 +26,7 @@ func (this *IndexAction) RunGet(params struct {
}
this.Data["webId"] = webConfig.Id
this.Data["cacheConfig"] = webConfig.CacheRefs
// 所有缓存策略
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.Data["cacheConfig"] = webConfig.Cache
this.Show()
}
@@ -53,10 +38,27 @@ func (this *IndexAction) RunPost(params struct {
Must *actions.Must
}) {
// TODO 校验配置
cacheConfig := &serverconfigs.HTTPCacheConfig{}
err := json.Unmarshal(params.CacheJSON, cacheConfig)
if err != nil {
this.ErrorPage(err)
return
}
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebCache(this.AdminContext(), &pb.UpdateHTTPWebCacheRequest{
// 去除不必要的部分
for _, cacheRef := range cacheConfig.CacheRefs {
cacheRef.CachePolicy = nil
}
cacheJSON, err := json.Marshal(cacheConfig)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebCache(this.AdminContext(), &pb.UpdateHTTPWebCacheRequest{
WebId: params.WebId,
CacheJSON: params.CacheJSON,
CacheJSON: cacheJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -100,7 +100,7 @@ func (this *LocationHelper) createMenus(serverIdString string, locationIdString
"name": "缓存",
"url": "/servers/server/settings/locations/cache?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "cache",
"isOn": locationConfig != nil && locationConfig.Web != nil && len(locationConfig.Web.CacheRefs) > 0,
"isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.Cache != nil && locationConfig.Web.Cache.IsOn && len(locationConfig.Web.Cache.CacheRefs) > 0,
})
menuItems = append(menuItems, maps.Map{
"name": "访问控制",

View File

@@ -211,7 +211,7 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"name": "缓存",
"url": "/servers/server/settings/cache?serverId=" + serverIdString,
"isActive": secondMenuItem == "cache",
"isOn": serverConfig.Web != nil && len(serverConfig.Web.CacheRefs) > 0,
"isOn": serverConfig.Web != nil && serverConfig.Web.Cache != nil && serverConfig.Web.Cache.IsOn && len(serverConfig.Web.Cache.CacheRefs) > 0,
})
menuItems = append(menuItems, maps.Map{
"name": "访问控制",

View File

@@ -0,0 +1,35 @@
package models
import (
"context"
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
var SharedHTTPCachePolicyDAO = new(HTTPCachePolicyDAO)
type HTTPCachePolicyDAO struct {
}
// 查找缓存策略配置
func (this *HTTPCachePolicyDAO) FindEnabledCachePolicyConfig(ctx context.Context, cachePolicyId int64) (*serverconfigs.HTTPCachePolicy, error) {
rpcClient, err := rpc.SharedRPC()
if err != nil {
return nil, err
}
resp, err := rpcClient.HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(ctx, &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: cachePolicyId})
if err != nil {
return nil, err
}
if len(resp.CachePolicyJSON) == 0 {
return nil, nil
}
config := &serverconfigs.HTTPCachePolicy{}
err = json.Unmarshal(resp.CachePolicyJSON, config)
if err != nil {
return nil, err
}
return config, nil
}