mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	实现修改缓存配置
This commit is contained in:
		@@ -66,6 +66,7 @@ func (this *APINode) listenRPC() error {
 | 
			
		||||
	pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{})
 | 
			
		||||
	pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{})
 | 
			
		||||
	pb.RegisterHTTPAccessLogPolicyServiceServer(rpcServer, &services.HTTPAccessLogPolicyService{})
 | 
			
		||||
	pb.RegisterHTTPCachePolicyServiceServer(rpcServer, &services.HTTPCachePolicyService{})
 | 
			
		||||
	err = rpcServer.Serve(listener)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return errors.New("[API]start rpc failed: " + err.Error())
 | 
			
		||||
 
 | 
			
		||||
@@ -63,3 +63,13 @@ func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(id int64) (string, error
 | 
			
		||||
		Result("name").
 | 
			
		||||
		FindStringCol("")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 查找所有可用的缓存策略
 | 
			
		||||
func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies() (result []*HTTPCachePolicy, err error) {
 | 
			
		||||
	_, err = this.Query().
 | 
			
		||||
		State(HTTPCachePolicyStateEnabled).
 | 
			
		||||
		DescPk().
 | 
			
		||||
		Slice(&result).
 | 
			
		||||
		FindAll()
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -69,14 +69,22 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
 | 
			
		||||
	if web == nil {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config := &serverconfigs.HTTPWebConfig{}
 | 
			
		||||
	config.Id = webId
 | 
			
		||||
	config.IsOn = web.IsOn == 1
 | 
			
		||||
	config.Root = web.Root
 | 
			
		||||
 | 
			
		||||
	// gzip
 | 
			
		||||
	if web.GzipId > 0 {
 | 
			
		||||
		gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(int64(web.GzipId))
 | 
			
		||||
	if IsNotNull(web.Gzip) {
 | 
			
		||||
		gzipRef := &serverconfigs.HTTPGzipRef{}
 | 
			
		||||
		err = json.Unmarshal([]byte(web.Gzip), gzipRef)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.GzipRef = gzipRef
 | 
			
		||||
 | 
			
		||||
		gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(gzipRef.GzipId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
@@ -138,22 +146,32 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
 | 
			
		||||
 | 
			
		||||
	// 访问日志
 | 
			
		||||
	if IsNotNull(web.AccessLog) {
 | 
			
		||||
		accessLogConfig := &serverconfigs.HTTPAccessLogConfig{}
 | 
			
		||||
		accessLogConfig := &serverconfigs.HTTPAccessLogRef{}
 | 
			
		||||
		err = json.Unmarshal([]byte(web.AccessLog), accessLogConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.AccessLog = accessLogConfig
 | 
			
		||||
		config.AccessLogRef = accessLogConfig
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 统计配置
 | 
			
		||||
	if IsNotNull(web.Stat) {
 | 
			
		||||
		statConfig := &serverconfigs.HTTPStatConfig{}
 | 
			
		||||
		err = json.Unmarshal([]byte(web.Stat), statConfig)
 | 
			
		||||
		statRef := &serverconfigs.HTTPStatRef{}
 | 
			
		||||
		err = json.Unmarshal([]byte(web.Stat), statRef)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.Stat = statConfig
 | 
			
		||||
		config.StatRef = statRef
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 缓存配置
 | 
			
		||||
	if IsNotNull(web.Cache) {
 | 
			
		||||
		cacheRef := &serverconfigs.HTTPCacheRef{}
 | 
			
		||||
		err = json.Unmarshal([]byte(web.Cache), cacheRef)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.CacheRef = cacheRef
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO 更多配置
 | 
			
		||||
@@ -190,13 +208,13 @@ func (this *HTTPWebDAO) UpdateWeb(webId int64, root string) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 修改Gzip配置
 | 
			
		||||
func (this *HTTPWebDAO) UpdateWebGzip(webId int64, gzipId int64) error {
 | 
			
		||||
func (this *HTTPWebDAO) UpdateWebGzip(webId int64, gzipJSON []byte) error {
 | 
			
		||||
	if webId <= 0 {
 | 
			
		||||
		return errors.New("invalid webId")
 | 
			
		||||
	}
 | 
			
		||||
	op := NewHTTPWebOperator()
 | 
			
		||||
	op.Id = webId
 | 
			
		||||
	op.GzipId = gzipId
 | 
			
		||||
	op.Gzip = gzipJSON
 | 
			
		||||
	_, err := this.Save(op)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -317,6 +335,22 @@ func (this *HTTPWebDAO) UpdateWebStat(webId int64, statJSON []byte) error {
 | 
			
		||||
	return this.NotifyUpdating(webId)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 更改缓存配置
 | 
			
		||||
func (this *HTTPWebDAO) UpdateWebCache(webId int64, cacheJSON []byte) error {
 | 
			
		||||
	if webId <= 0 {
 | 
			
		||||
		return errors.New("invalid webId")
 | 
			
		||||
	}
 | 
			
		||||
	op := NewHTTPWebOperator()
 | 
			
		||||
	op.Id = webId
 | 
			
		||||
	op.Cache = JSONBytes(cacheJSON)
 | 
			
		||||
	_, err := this.Save(op)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.NotifyUpdating(webId)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 通知更新
 | 
			
		||||
func (this *HTTPWebDAO) NotifyUpdating(webId int64) error {
 | 
			
		||||
	err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId)
 | 
			
		||||
 
 | 
			
		||||
@@ -10,20 +10,18 @@ type HTTPWeb struct {
 | 
			
		||||
	State                  uint8  `field:"state"`                  // 状态
 | 
			
		||||
	CreatedAt              uint32 `field:"createdAt"`              // 创建时间
 | 
			
		||||
	Root                   string `field:"root"`                   // 资源根目录
 | 
			
		||||
	GzipId                 uint32 `field:"gzipId"`                 // Gzip配置
 | 
			
		||||
	Charset                string `field:"charset"`                // 字符集
 | 
			
		||||
	Shutdown               string `field:"shutdown"`               // 临时关闭页面配置
 | 
			
		||||
	Pages                  string `field:"pages"`                  // 特殊页面
 | 
			
		||||
	FirewallId             uint32 `field:"firewallId"`             // WAF ID
 | 
			
		||||
	CachePolicyId          uint32 `field:"cachePolicyId"`          // 缓存策略ID
 | 
			
		||||
	RedirectToHttps        string `field:"redirectToHttps"`        // 跳转到HTTPS设置
 | 
			
		||||
	Indexes                string `field:"indexes"`                // 首页文件列表
 | 
			
		||||
	MaxRequestBodySize     string `field:"maxRequestBodySize"`     // 最大允许的请求内容尺寸
 | 
			
		||||
	StatPolicyId           uint32 `field:"statPolicyId"`           // 统计策略ID
 | 
			
		||||
	RequestHeaderPolicyId  uint32 `field:"requestHeaderPolicyId"`  // Request Header策略ID
 | 
			
		||||
	ResponseHeaderPolicyId uint32 `field:"responseHeaderPolicyId"` // Response Header策略
 | 
			
		||||
	AccessLog              string `field:"accessLog"`              // 访问日志配置
 | 
			
		||||
	Stat                   string `field:"stat"`                   // 统计配置
 | 
			
		||||
	Gzip                   string `field:"gzip"`                   // Gzip配置
 | 
			
		||||
	Cache                  string `field:"cache"`                  // 缓存配置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPWebOperator struct {
 | 
			
		||||
@@ -35,20 +33,18 @@ type HTTPWebOperator struct {
 | 
			
		||||
	State                  interface{} // 状态
 | 
			
		||||
	CreatedAt              interface{} // 创建时间
 | 
			
		||||
	Root                   interface{} // 资源根目录
 | 
			
		||||
	GzipId                 interface{} // Gzip配置
 | 
			
		||||
	Charset                interface{} // 字符集
 | 
			
		||||
	Shutdown               interface{} // 临时关闭页面配置
 | 
			
		||||
	Pages                  interface{} // 特殊页面
 | 
			
		||||
	FirewallId             interface{} // WAF ID
 | 
			
		||||
	CachePolicyId          interface{} // 缓存策略ID
 | 
			
		||||
	RedirectToHttps        interface{} // 跳转到HTTPS设置
 | 
			
		||||
	Indexes                interface{} // 首页文件列表
 | 
			
		||||
	MaxRequestBodySize     interface{} // 最大允许的请求内容尺寸
 | 
			
		||||
	StatPolicyId           interface{} // 统计策略ID
 | 
			
		||||
	RequestHeaderPolicyId  interface{} // Request Header策略ID
 | 
			
		||||
	ResponseHeaderPolicyId interface{} // Response Header策略
 | 
			
		||||
	AccessLog              interface{} // 访问日志配置
 | 
			
		||||
	Stat                   interface{} // 统计配置
 | 
			
		||||
	Gzip                   interface{} // Gzip配置
 | 
			
		||||
	Cache                  interface{} // 缓存配置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHTTPWebOperator() *HTTPWebOperator {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										34
									
								
								internal/rpc/services/service_http_cache_policy.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								internal/rpc/services/service_http_cache_policy.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
package services
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type HTTPCachePolicyService struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 获取所有可用策略
 | 
			
		||||
func (this *HTTPCachePolicyService) FindAllEnabledHTTPCachePolicies(ctx context.Context, req *pb.FindAllEnabledHTTPCachePoliciesRequest) (*pb.FindAllEnabledHTTPCachePoliciesResponse, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	policies, err := models.SharedHTTPCachePolicyDAO.FindAllEnabledCachePolicies()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	result := []*pb.HTTPCachePolicy{}
 | 
			
		||||
	for _, p := range policies {
 | 
			
		||||
		result = append(result, &pb.HTTPCachePolicy{
 | 
			
		||||
			Id:   int64(p.Id),
 | 
			
		||||
			Name: p.Name,
 | 
			
		||||
			IsOn: p.IsOn == 1,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.FindAllEnabledHTTPCachePoliciesResponse{CachePolicies: result}, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -47,7 +47,6 @@ func (this *HTTPWebService) FindEnabledHTTPWeb(ctx context.Context, req *pb.Find
 | 
			
		||||
	result.Id = int64(web.Id)
 | 
			
		||||
	result.IsOn = web.IsOn == 1
 | 
			
		||||
	result.Root = web.Root
 | 
			
		||||
	result.GzipId = int64(web.GzipId)
 | 
			
		||||
	result.Charset = web.Charset
 | 
			
		||||
	result.RequestHeaderPolicyId = int64(web.RequestHeaderPolicyId)
 | 
			
		||||
	result.ResponseHeaderPolicyId = int64(web.ResponseHeaderPolicyId)
 | 
			
		||||
@@ -78,7 +77,7 @@ func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.Updat
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedHTTPWebDAO.UpdateWebGzip(req.WebId, req.GzipId)
 | 
			
		||||
	err = models.SharedHTTPWebDAO.UpdateWebGzip(req.WebId, req.GzipJSON)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -192,3 +191,19 @@ func (this *HTTPWebService) UpdateHTTPStat(ctx context.Context, req *pb.UpdateHT
 | 
			
		||||
	}
 | 
			
		||||
	return rpcutils.RPCUpdateSuccess()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 更改缓存配置
 | 
			
		||||
func (this *HTTPWebService) UpdateHTTPCache(ctx context.Context, req *pb.UpdateHTTPCacheRequest) (*pb.RPCUpdateSuccess, error) {
 | 
			
		||||
	// 校验请求
 | 
			
		||||
	_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedHTTPWebDAO.UpdateWebCache(req.WebId, req.CacheJSON)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return rpcutils.RPCUpdateSuccess()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user