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

118 lines
2.8 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/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
2020-10-02 17:22:24 +08:00
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
2020-10-02 17:22:24 +08:00
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
2020-10-04 20:38:27 +08:00
this.Data["types"] = serverconfigs.AllCachePolicyStorageTypes
2020-10-02 17:22:24 +08:00
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
Name string
Type string
// file
FileDir string
FileMemoryCapacityJSON []byte
2022-01-12 21:09:11 +08:00
FileOpenFileCacheMax int
2020-10-02 17:22:24 +08:00
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("请输入缓存目录")
memoryCapacity := &shared.SizeCapacity{}
if len(params.FileMemoryCapacityJSON) > 0 {
err := json.Unmarshal(params.FileMemoryCapacityJSON, memoryCapacity)
if err != nil {
this.ErrorPage(err)
return
}
}
2022-01-12 21:09:11 +08:00
var openFileCacheConfig *serverconfigs.OpenFileCacheConfig
if params.FileOpenFileCacheMax > 0 {
openFileCacheConfig = &serverconfigs.OpenFileCacheConfig{
IsOn: true,
Max: params.FileOpenFileCacheMax,
}
}
2020-10-04 20:38:27 +08:00
options = &serverconfigs.HTTPFileCacheStorage{
2020-10-02 17:22:24 +08:00
Dir: params.FileDir,
MemoryPolicy: &serverconfigs.HTTPCachePolicy{
Capacity: memoryCapacity,
},
2022-01-12 21:09:11 +08:00
OpenFileCache: openFileCacheConfig,
2020-10-02 17:22:24 +08:00
}
2020-10-04 20:38:27 +08:00
case serverconfigs.CachePolicyStorageMemory:
2022-01-12 21:09:11 +08:00
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
}
2020-11-10 21:37:48 +08:00
createResp, err := this.RPC().HTTPCachePolicyRPC().CreateHTTPCachePolicy(this.AdminContext(), &pb.CreateHTTPCachePolicyRequest{
2020-10-02 17:22:24 +08:00
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
}
// 返回数据
this.Data["cachePolicy"] = maps.Map{
"id": createResp.HttpCachePolicyId,
"name": params.Name,
}
2020-11-10 21:37:48 +08:00
// 创建日志
defer this.CreateLog(oplogs.LevelInfo, "创建缓存策略:%d", createResp.HttpCachePolicyId)
2020-11-10 21:37:48 +08:00
2020-10-02 17:22:24 +08:00
this.Success()
}