diff --git a/internal/apis/api_node.go b/internal/apis/api_node.go index e9ea8d02..1c0388a4 100644 --- a/internal/apis/api_node.go +++ b/internal/apis/api_node.go @@ -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()) diff --git a/internal/db/models/http_cache_policy_dao.go b/internal/db/models/http_cache_policy_dao.go index a39dfae5..a253336a 100644 --- a/internal/db/models/http_cache_policy_dao.go +++ b/internal/db/models/http_cache_policy_dao.go @@ -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 +} diff --git a/internal/db/models/http_web_dao.go b/internal/db/models/http_web_dao.go index eb93a46d..28a9deb9 100644 --- a/internal/db/models/http_web_dao.go +++ b/internal/db/models/http_web_dao.go @@ -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) diff --git a/internal/db/models/http_web_model.go b/internal/db/models/http_web_model.go index 8b816314..14e6a39d 100644 --- a/internal/db/models/http_web_model.go +++ b/internal/db/models/http_web_model.go @@ -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 { diff --git a/internal/rpc/services/service_http_cache_policy.go b/internal/rpc/services/service_http_cache_policy.go new file mode 100644 index 00000000..605933c9 --- /dev/null +++ b/internal/rpc/services/service_http_cache_policy.go @@ -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 +} diff --git a/internal/rpc/services/service_http_web.go b/internal/rpc/services/service_http_web.go index b690595f..2b5b21a1 100644 --- a/internal/rpc/services/service_http_web.go +++ b/internal/rpc/services/service_http_web.go @@ -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() +}