2020-08-21 21:09:42 +08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
2020-10-04 20:38:27 +08:00
|
|
|
"encoding/json"
|
2020-11-20 15:32:42 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
|
2020-08-21 21:09:42 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2020-12-23 09:52:31 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
2020-09-20 16:28:16 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-10-04 20:38:27 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
2020-09-20 16:28:16 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
2020-12-17 15:50:44 +08:00
|
|
|
"github.com/iwind/TeaGo/maps"
|
2021-10-07 16:47:14 +08:00
|
|
|
"github.com/iwind/TeaGo/types"
|
2020-08-21 21:09:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IndexAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) Init() {
|
|
|
|
|
this.Nav("", "setting", "index")
|
|
|
|
|
this.SecondMenu("cache")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
|
|
|
ServerId int64
|
|
|
|
|
}) {
|
2021-10-07 16:47:14 +08:00
|
|
|
// 服务分组设置
|
|
|
|
|
groupResp, err := this.RPC().ServerGroupRPC().FindEnabledServerGroupConfigInfo(this.AdminContext(), &pb.FindEnabledServerGroupConfigInfoRequest{
|
|
|
|
|
ServerId: params.ServerId,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.Data["hasGroupConfig"] = groupResp.HasCacheConfig
|
|
|
|
|
this.Data["groupSettingURL"] = "/servers/groups/group/settings/cache?groupId=" + types.String(groupResp.ServerGroupId)
|
|
|
|
|
|
2021-01-03 20:25:15 +08:00
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
2020-09-20 16:28:16 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Data["webId"] = webConfig.Id
|
2020-10-04 20:38:27 +08:00
|
|
|
this.Data["cacheConfig"] = webConfig.Cache
|
2020-08-21 21:09:42 +08:00
|
|
|
|
2020-12-17 15:50:44 +08:00
|
|
|
// 当前集群的缓存策略
|
2020-12-23 09:52:31 +08:00
|
|
|
cachePolicy, err := dao.SharedHTTPCachePolicyDAO.FindEnabledHTTPCachePolicyWithServerId(this.AdminContext(), params.ServerId)
|
2020-12-17 15:50:44 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if cachePolicy != nil {
|
|
|
|
|
this.Data["cachePolicy"] = maps.Map{
|
|
|
|
|
"id": cachePolicy.Id,
|
|
|
|
|
"name": cachePolicy.Name,
|
|
|
|
|
"isOn": cachePolicy.IsOn,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.Data["cachePolicy"] = nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
this.Show()
|
|
|
|
|
}
|
2020-09-20 16:28:16 +08:00
|
|
|
|
|
|
|
|
func (this *IndexAction) RunPost(params struct {
|
|
|
|
|
WebId int64
|
|
|
|
|
CacheJSON []byte
|
|
|
|
|
|
|
|
|
|
Must *actions.Must
|
|
|
|
|
}) {
|
2020-11-20 15:32:42 +08:00
|
|
|
// 日志
|
|
|
|
|
defer this.CreateLog(oplogs.LevelInfo, "修改Web %d 的缓存设置", params.WebId)
|
|
|
|
|
|
2021-05-12 16:09:40 +08:00
|
|
|
// 校验配置
|
2020-10-04 20:38:27 +08:00
|
|
|
cacheConfig := &serverconfigs.HTTPCacheConfig{}
|
|
|
|
|
err := json.Unmarshal(params.CacheJSON, cacheConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-05-12 16:09:40 +08:00
|
|
|
err = cacheConfig.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.Fail("检查配置失败:" + err.Error())
|
|
|
|
|
}
|
2020-10-04 20:38:27 +08:00
|
|
|
|
|
|
|
|
// 去除不必要的部分
|
|
|
|
|
for _, cacheRef := range cacheConfig.CacheRefs {
|
|
|
|
|
cacheRef.CachePolicy = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cacheJSON, err := json.Marshal(cacheConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-20 16:28:16 +08:00
|
|
|
|
2020-10-04 20:38:27 +08:00
|
|
|
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebCache(this.AdminContext(), &pb.UpdateHTTPWebCacheRequest{
|
2020-09-20 16:28:16 +08:00
|
|
|
WebId: params.WebId,
|
2020-10-04 20:38:27 +08:00
|
|
|
CacheJSON: cacheJSON,
|
2020-09-20 16:28:16 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|