mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			122 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			122 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package cache
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	"github.com/TeaOSLab/EdgeAdmin/internal/utils"
 | 
						|
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
						|
	"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
 | 
						|
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
 | 
						|
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
						|
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
						|
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
 | 
						|
	"github.com/iwind/TeaGo/actions"
 | 
						|
	"github.com/iwind/TeaGo/maps"
 | 
						|
	"github.com/iwind/TeaGo/types"
 | 
						|
)
 | 
						|
 | 
						|
type IndexAction struct {
 | 
						|
	actionutils.ParentAction
 | 
						|
}
 | 
						|
 | 
						|
func (this *IndexAction) Init() {
 | 
						|
	this.Nav("", "setting", "index")
 | 
						|
	this.SecondMenu("cache")
 | 
						|
}
 | 
						|
 | 
						|
func (this *IndexAction) RunGet(params struct {
 | 
						|
	ServerId int64
 | 
						|
}) {
 | 
						|
	// 服务分组设置
 | 
						|
	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)
 | 
						|
 | 
						|
	webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
 | 
						|
	if err != nil {
 | 
						|
		this.ErrorPage(err)
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	this.Data["webId"] = webConfig.Id
 | 
						|
	this.Data["cacheConfig"] = webConfig.Cache
 | 
						|
 | 
						|
	// 当前集群的缓存策略
 | 
						|
	cachePolicy, err := dao.SharedHTTPCachePolicyDAO.FindEnabledHTTPCachePolicyWithServerId(this.AdminContext(), params.ServerId)
 | 
						|
	if err != nil {
 | 
						|
		this.ErrorPage(err)
 | 
						|
		return
 | 
						|
	}
 | 
						|
	if cachePolicy != nil {
 | 
						|
		var maxBytes = &shared.SizeCapacity{}
 | 
						|
		if !utils.JSONIsNull(cachePolicy.MaxBytesJSON) {
 | 
						|
			err = json.Unmarshal(cachePolicy.MaxBytesJSON, maxBytes)
 | 
						|
			if err != nil {
 | 
						|
				this.ErrorPage(err)
 | 
						|
				return
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		this.Data["cachePolicy"] = maps.Map{
 | 
						|
			"id":       cachePolicy.Id,
 | 
						|
			"name":     cachePolicy.Name,
 | 
						|
			"isOn":     cachePolicy.IsOn,
 | 
						|
			"maxBytes": maxBytes,
 | 
						|
		}
 | 
						|
	} else {
 | 
						|
		this.Data["cachePolicy"] = nil
 | 
						|
	}
 | 
						|
 | 
						|
	this.Show()
 | 
						|
}
 | 
						|
 | 
						|
func (this *IndexAction) RunPost(params struct {
 | 
						|
	WebId     int64
 | 
						|
	CacheJSON []byte
 | 
						|
 | 
						|
	Must *actions.Must
 | 
						|
}) {
 | 
						|
	// 日志
 | 
						|
	defer this.CreateLogInfo(codes.ServerCache_LogUpdateCacheSettings, params.WebId)
 | 
						|
 | 
						|
	// 校验配置
 | 
						|
	var cacheConfig = &serverconfigs.HTTPCacheConfig{}
 | 
						|
	err := json.Unmarshal(params.CacheJSON, cacheConfig)
 | 
						|
	if err != nil {
 | 
						|
		this.ErrorPage(err)
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	err = cacheConfig.Init()
 | 
						|
	if err != nil {
 | 
						|
		this.Fail("检查配置失败:" + err.Error())
 | 
						|
	}
 | 
						|
 | 
						|
	// 去除不必要的部分
 | 
						|
	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{
 | 
						|
		HttpWebId: params.WebId,
 | 
						|
		CacheJSON: cacheJSON,
 | 
						|
	})
 | 
						|
	if err != nil {
 | 
						|
		this.ErrorPage(err)
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	this.Success()
 | 
						|
}
 |