diff --git a/internal/const/const.go b/internal/const/const.go index f9daf4ef..3adc7576 100644 --- a/internal/const/const.go +++ b/internal/const/const.go @@ -22,4 +22,5 @@ const ( UserNodeVersion = "0.0.7" AuthorityNodeVersion = "0.0.1" MonitorNodeVersion = "0.0.1" + DNSNodeVersion = "0.0.1" ) diff --git a/internal/db/models/http_cache_policy_dao.go b/internal/db/models/http_cache_policy_dao.go index bf7b5775..c0e4414f 100644 --- a/internal/db/models/http_cache_policy_dao.go +++ b/internal/db/models/http_cache_policy_dao.go @@ -37,12 +37,12 @@ func init() { }) } -// 初始化 +// Init 初始化 func (this *HTTPCachePolicyDAO) Init() { _ = this.DAOObject.Init() } -// 启用条目 +// EnableHTTPCachePolicy 启用条目 func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) error { _, err := this.Query(tx). Pk(id). @@ -51,7 +51,7 @@ func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) erro return err } -// 禁用条目 +// DisableHTTPCachePolicy 禁用条目 func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int64) error { _, err := this.Query(tx). Pk(policyId). @@ -63,7 +63,7 @@ func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int6 return this.NotifyUpdate(tx, policyId) } -// 查找启用中的条目 +// FindEnabledHTTPCachePolicy 查找启用中的条目 func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64) (*HTTPCachePolicy, error) { result, err := this.Query(tx). Pk(id). @@ -75,7 +75,7 @@ func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64) return result.(*HTTPCachePolicy), err } -// 根据主键查找名称 +// FindHTTPCachePolicyName 根据主键查找名称 func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(tx *dbs.Tx, id int64) (string, error) { return this.Query(tx). Pk(id). @@ -83,7 +83,7 @@ func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(tx *dbs.Tx, id int64) (s FindStringCol("") } -// 查找所有可用的缓存策略 +// FindAllEnabledCachePolicies 查找所有可用的缓存策略 func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result []*HTTPCachePolicy, err error) { _, err = this.Query(tx). State(HTTPCachePolicyStateEnabled). @@ -93,7 +93,7 @@ func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result return } -// 创建缓存策略 +// CreateCachePolicy 创建缓存策略 func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) (int64, error) { op := NewHTTPCachePolicyOperator() op.State = HTTPCachePolicyStateEnabled @@ -118,7 +118,7 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st return types.Int64(op.Id), nil } -// 修改缓存策略 +// UpdateCachePolicy 修改缓存策略 func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) error { if policyId <= 0 { return errors.New("invalid policyId") @@ -147,7 +147,7 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is return this.NotifyUpdate(tx, policyId) } -// 组合配置 +// ComposeCachePolicy 组合配置 func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (*serverconfigs.HTTPCachePolicy, error) { policy, err := this.FindEnabledHTTPCachePolicy(tx, policyId) if err != nil { @@ -196,17 +196,27 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) ( config.Options = m } + // refs + if IsNotNull(policy.Refs) { + refs := []*serverconfigs.HTTPCacheRef{} + err = json.Unmarshal([]byte(policy.Refs), &refs) + if err != nil { + return nil, err + } + config.CacheRefs = refs + } + return config, nil } -// 计算可用缓存策略数量 +// CountAllEnabledHTTPCachePolicies 计算可用缓存策略数量 func (this *HTTPCachePolicyDAO) CountAllEnabledHTTPCachePolicies(tx *dbs.Tx) (int64, error) { return this.Query(tx). State(HTTPCachePolicyStateEnabled). Count() } -// 列出单页的缓存策略 +// ListEnabledHTTPCachePolicies 列出单页的缓存策略 func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset int64, size int64) ([]*serverconfigs.HTTPCachePolicy, error) { ones, err := this.Query(tx). State(HTTPCachePolicyStateEnabled). @@ -237,7 +247,22 @@ func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset return cachePolicies, nil } -// 通知更新 +// UpdatePolicyRefs 设置默认的缓存条件 +func (this *HTTPCachePolicyDAO) UpdatePolicyRefs(tx *dbs.Tx, policyId int64, refsJSON []byte) error { + if len(refsJSON) == 0 { + return nil + } + err := this.Query(tx). + Pk(policyId). + Set("refs", refsJSON). + UpdateQuickly() + if err != nil { + return err + } + return this.NotifyUpdate(tx, policyId) +} + +// NotifyUpdate 通知更新 func (this *HTTPCachePolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error { webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithCachePolicyId(tx, policyId) if err != nil { diff --git a/internal/db/models/http_cache_policy_model.go b/internal/db/models/http_cache_policy_model.go index 27f21811..88a71d64 100644 --- a/internal/db/models/http_cache_policy_model.go +++ b/internal/db/models/http_cache_policy_model.go @@ -1,6 +1,6 @@ package models -// HTTP缓存策略 +// HTTPCachePolicy HTTP缓存策略 type HTTPCachePolicy struct { Id uint32 `field:"id"` // ID AdminId uint32 `field:"adminId"` // 管理员ID @@ -16,6 +16,7 @@ type HTTPCachePolicy struct { CreatedAt uint64 `field:"createdAt"` // 创建时间 State uint8 `field:"state"` // 状态 Description string `field:"description"` // 描述 + Refs string `field:"refs"` // 默认的缓存设置 } type HTTPCachePolicyOperator struct { @@ -33,6 +34,7 @@ type HTTPCachePolicyOperator struct { CreatedAt interface{} // 创建时间 State interface{} // 状态 Description interface{} // 描述 + Refs interface{} // 默认的缓存设置 } func NewHTTPCachePolicyOperator() *HTTPCachePolicyOperator { diff --git a/internal/rpc/services/service_http_cache_policy.go b/internal/rpc/services/service_http_cache_policy.go index 1e6ceefb..98445aca 100644 --- a/internal/rpc/services/service_http_cache_policy.go +++ b/internal/rpc/services/service_http_cache_policy.go @@ -12,7 +12,7 @@ type HTTPCachePolicyService struct { BaseService } -// 获取所有可用策略 +// FindAllEnabledHTTPCachePolicies 获取所有可用策略 func (this *HTTPCachePolicyService) FindAllEnabledHTTPCachePolicies(ctx context.Context, req *pb.FindAllEnabledHTTPCachePoliciesRequest) (*pb.FindAllEnabledHTTPCachePoliciesResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -37,7 +37,7 @@ func (this *HTTPCachePolicyService) FindAllEnabledHTTPCachePolicies(ctx context. return &pb.FindAllEnabledHTTPCachePoliciesResponse{CachePolicies: result}, nil } -// 创建缓存策略 +// CreateHTTPCachePolicy 创建缓存策略 func (this *HTTPCachePolicyService) CreateHTTPCachePolicy(ctx context.Context, req *pb.CreateHTTPCachePolicyRequest) (*pb.CreateHTTPCachePolicyResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -54,7 +54,7 @@ func (this *HTTPCachePolicyService) CreateHTTPCachePolicy(ctx context.Context, r return &pb.CreateHTTPCachePolicyResponse{HttpCachePolicyId: policyId}, nil } -// 修改缓存策略 +// UpdateHTTPCachePolicy 修改缓存策略 func (this *HTTPCachePolicyService) UpdateHTTPCachePolicy(ctx context.Context, req *pb.UpdateHTTPCachePolicyRequest) (*pb.RPCSuccess, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -72,7 +72,7 @@ func (this *HTTPCachePolicyService) UpdateHTTPCachePolicy(ctx context.Context, r return this.Success() } -// 删除缓存策略 +// DeleteHTTPCachePolicy 删除缓存策略 func (this *HTTPCachePolicyService) DeleteHTTPCachePolicy(ctx context.Context, req *pb.DeleteHTTPCachePolicyRequest) (*pb.RPCSuccess, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -90,7 +90,7 @@ func (this *HTTPCachePolicyService) DeleteHTTPCachePolicy(ctx context.Context, r return this.Success() } -// 计算缓存策略数量 +// CountAllEnabledHTTPCachePolicies 计算缓存策略数量 func (this *HTTPCachePolicyService) CountAllEnabledHTTPCachePolicies(ctx context.Context, req *pb.CountAllEnabledHTTPCachePoliciesRequest) (*pb.RPCCountResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -107,7 +107,7 @@ func (this *HTTPCachePolicyService) CountAllEnabledHTTPCachePolicies(ctx context return this.SuccessCount(count) } -// 列出单页的缓存策略 +// ListEnabledHTTPCachePolicies 列出单页的缓存策略 func (this *HTTPCachePolicyService) ListEnabledHTTPCachePolicies(ctx context.Context, req *pb.ListEnabledHTTPCachePoliciesRequest) (*pb.ListEnabledHTTPCachePoliciesResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -128,7 +128,7 @@ func (this *HTTPCachePolicyService) ListEnabledHTTPCachePolicies(ctx context.Con return &pb.ListEnabledHTTPCachePoliciesResponse{HttpCachePoliciesJSON: cachePoliciesJSON}, nil } -// 查找单个缓存策略配置 +// FindEnabledHTTPCachePolicyConfig 查找单个缓存策略配置 func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicyConfig(ctx context.Context, req *pb.FindEnabledHTTPCachePolicyConfigRequest) (*pb.FindEnabledHTTPCachePolicyConfigResponse, error) { // 校验请求 _, _, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -146,7 +146,7 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicyConfig(ctx context return &pb.FindEnabledHTTPCachePolicyConfigResponse{HttpCachePolicyJSON: cachePolicyJSON}, nil } -// 查找单个缓存策略信息 +// FindEnabledHTTPCachePolicy 查找单个缓存策略信息 func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Context, req *pb.FindEnabledHTTPCachePolicyRequest) (*pb.FindEnabledHTTPCachePolicyResponse, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { @@ -168,3 +168,18 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Conte IsOn: policy.IsOn == 1, }}, nil } + +// UpdateHTTPCachePolicyRefs 设置缓存策略的默认条件 +func (this *HTTPCachePolicyService) UpdateHTTPCachePolicyRefs(ctx context.Context, req *pb.UpdateHTTPCachePolicyRefsRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + tx := this.NullTx() + err = models.SharedHTTPCachePolicyDAO.UpdatePolicyRefs(tx, req.HttpCachePolicyId, req.RefsJSON) + if err != nil { + return nil, err + } + return this.Success() +}