Files
EdgeAdmin/internal/web/actions/default/servers/components/cache/update.go

113 lines
2.5 KiB
Go
Raw Normal View History

2020-10-02 17:22:24 +08:00
package cache
import (
"encoding/json"
2020-11-10 21:37:48 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
2020-10-02 17:22:24 +08:00
"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"
)
2020-10-04 14:27:05 +08:00
type UpdateAction struct {
2020-10-02 17:22:24 +08:00
actionutils.ParentAction
}
2020-10-04 14:27:05 +08:00
func (this *UpdateAction) Init() {
this.Nav("", "", "update")
2020-10-02 17:22:24 +08:00
}
2020-10-04 14:27:05 +08:00
func (this *UpdateAction) RunGet(params struct {
2020-10-02 17:22:24 +08:00
CachePolicyId int64
}) {
configResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{CachePolicyId: params.CachePolicyId})
if err != nil {
this.ErrorPage(err)
return
}
configJSON := configResp.CachePolicyJSON
if len(configJSON) == 0 {
this.NotFound("cachePolicy", params.CachePolicyId)
return
}
cachePolicy := &serverconfigs.HTTPCachePolicy{}
err = json.Unmarshal(configJSON, cachePolicy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["cachePolicy"] = cachePolicy
// 其他选项
2020-10-04 20:38:27 +08:00
this.Data["types"] = serverconfigs.AllCachePolicyStorageTypes
2020-10-02 17:22:24 +08:00
this.Show()
}
2020-10-04 14:27:05 +08:00
func (this *UpdateAction) RunPost(params struct {
2020-10-02 17:22:24 +08:00
CachePolicyId int64
Name string
Type string
// file
FileDir string
CapacityJSON []byte
MaxSizeJSON []byte
MaxKeys int64
Description string
IsOn bool
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入策略名称")
// 校验选项
var options interface{}
switch params.Type {
2020-10-04 20:38:27 +08:00
case serverconfigs.CachePolicyStorageFile:
2020-10-02 17:22:24 +08:00
params.Must.
Field("fileDir", params.FileDir).
Require("请输入缓存目录")
2020-10-04 20:38:27 +08:00
options = &serverconfigs.HTTPFileCacheStorage{
2020-10-02 17:22:24 +08:00
Dir: params.FileDir,
}
2020-10-04 20:38:27 +08:00
case serverconfigs.CachePolicyStorageMemory:
options = &serverconfigs.HTTPMemoryCacheStorage{
2020-10-02 17:22:24 +08:00
}
default:
this.Fail("请选择正确的缓存类型")
}
optionsJSON, err := json.Marshal(options)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPCachePolicyRPC().UpdateHTTPCachePolicy(this.AdminContext(), &pb.UpdateHTTPCachePolicyRequest{
CachePolicyId: params.CachePolicyId,
IsOn: params.IsOn,
Name: params.Name,
Description: params.Description,
CapacityJSON: params.CapacityJSON,
MaxKeys: params.MaxKeys,
MaxSizeJSON: params.MaxSizeJSON,
Type: params.Type,
OptionsJSON: optionsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
2020-11-10 21:37:48 +08:00
// 创建日志
this.CreateLog(oplogs.LevelInfo, "修改缓存策略:%d", params.CachePolicyId)
2020-10-02 17:22:24 +08:00
this.Success()
}