实现修改缓存配置

This commit is contained in:
刘祥超
2020-09-20 16:28:07 +08:00
parent bbc21b1cd2
commit 089b30e095
6 changed files with 109 additions and 19 deletions

View File

@@ -66,6 +66,7 @@ func (this *APINode) listenRPC() error {
pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{}) pb.RegisterHTTPHeaderServiceServer(rpcServer, &services.HTTPHeaderService{})
pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{}) pb.RegisterHTTPPageServiceServer(rpcServer, &services.HTTPPageService{})
pb.RegisterHTTPAccessLogPolicyServiceServer(rpcServer, &services.HTTPAccessLogPolicyService{}) pb.RegisterHTTPAccessLogPolicyServiceServer(rpcServer, &services.HTTPAccessLogPolicyService{})
pb.RegisterHTTPCachePolicyServiceServer(rpcServer, &services.HTTPCachePolicyService{})
err = rpcServer.Serve(listener) err = rpcServer.Serve(listener)
if err != nil { if err != nil {
return errors.New("[API]start rpc failed: " + err.Error()) return errors.New("[API]start rpc failed: " + err.Error())

View File

@@ -63,3 +63,13 @@ func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(id int64) (string, error
Result("name"). Result("name").
FindStringCol("") FindStringCol("")
} }
// 查找所有可用的缓存策略
func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies() (result []*HTTPCachePolicy, err error) {
_, err = this.Query().
State(HTTPCachePolicyStateEnabled).
DescPk().
Slice(&result).
FindAll()
return
}

View File

@@ -69,14 +69,22 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
if web == nil { if web == nil {
return nil, nil return nil, nil
} }
config := &serverconfigs.HTTPWebConfig{} config := &serverconfigs.HTTPWebConfig{}
config.Id = webId config.Id = webId
config.IsOn = web.IsOn == 1 config.IsOn = web.IsOn == 1
config.Root = web.Root config.Root = web.Root
// gzip // gzip
if web.GzipId > 0 { if IsNotNull(web.Gzip) {
gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(int64(web.GzipId)) 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 { if err != nil {
return nil, err return nil, err
} }
@@ -138,22 +146,32 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
// 访问日志 // 访问日志
if IsNotNull(web.AccessLog) { if IsNotNull(web.AccessLog) {
accessLogConfig := &serverconfigs.HTTPAccessLogConfig{} accessLogConfig := &serverconfigs.HTTPAccessLogRef{}
err = json.Unmarshal([]byte(web.AccessLog), accessLogConfig) err = json.Unmarshal([]byte(web.AccessLog), accessLogConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
config.AccessLog = accessLogConfig config.AccessLogRef = accessLogConfig
} }
// 统计配置 // 统计配置
if IsNotNull(web.Stat) { if IsNotNull(web.Stat) {
statConfig := &serverconfigs.HTTPStatConfig{} statRef := &serverconfigs.HTTPStatRef{}
err = json.Unmarshal([]byte(web.Stat), statConfig) err = json.Unmarshal([]byte(web.Stat), statRef)
if err != nil { if err != nil {
return nil, err 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 更多配置 // TODO 更多配置
@@ -190,13 +208,13 @@ func (this *HTTPWebDAO) UpdateWeb(webId int64, root string) error {
} }
// 修改Gzip配置 // 修改Gzip配置
func (this *HTTPWebDAO) UpdateWebGzip(webId int64, gzipId int64) error { func (this *HTTPWebDAO) UpdateWebGzip(webId int64, gzipJSON []byte) error {
if webId <= 0 { if webId <= 0 {
return errors.New("invalid webId") return errors.New("invalid webId")
} }
op := NewHTTPWebOperator() op := NewHTTPWebOperator()
op.Id = webId op.Id = webId
op.GzipId = gzipId op.Gzip = gzipJSON
_, err := this.Save(op) _, err := this.Save(op)
if err != nil { if err != nil {
return err return err
@@ -317,6 +335,22 @@ func (this *HTTPWebDAO) UpdateWebStat(webId int64, statJSON []byte) error {
return this.NotifyUpdating(webId) 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 { func (this *HTTPWebDAO) NotifyUpdating(webId int64) error {
err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId) err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId)

View File

@@ -10,20 +10,18 @@ type HTTPWeb struct {
State uint8 `field:"state"` // 状态 State uint8 `field:"state"` // 状态
CreatedAt uint32 `field:"createdAt"` // 创建时间 CreatedAt uint32 `field:"createdAt"` // 创建时间
Root string `field:"root"` // 资源根目录 Root string `field:"root"` // 资源根目录
GzipId uint32 `field:"gzipId"` // Gzip配置
Charset string `field:"charset"` // 字符集 Charset string `field:"charset"` // 字符集
Shutdown string `field:"shutdown"` // 临时关闭页面配置 Shutdown string `field:"shutdown"` // 临时关闭页面配置
Pages string `field:"pages"` // 特殊页面 Pages string `field:"pages"` // 特殊页面
FirewallId uint32 `field:"firewallId"` // WAF ID
CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID
RedirectToHttps string `field:"redirectToHttps"` // 跳转到HTTPS设置 RedirectToHttps string `field:"redirectToHttps"` // 跳转到HTTPS设置
Indexes string `field:"indexes"` // 首页文件列表 Indexes string `field:"indexes"` // 首页文件列表
MaxRequestBodySize string `field:"maxRequestBodySize"` // 最大允许的请求内容尺寸 MaxRequestBodySize string `field:"maxRequestBodySize"` // 最大允许的请求内容尺寸
StatPolicyId uint32 `field:"statPolicyId"` // 统计策略ID
RequestHeaderPolicyId uint32 `field:"requestHeaderPolicyId"` // Request Header策略ID RequestHeaderPolicyId uint32 `field:"requestHeaderPolicyId"` // Request Header策略ID
ResponseHeaderPolicyId uint32 `field:"responseHeaderPolicyId"` // Response Header策略 ResponseHeaderPolicyId uint32 `field:"responseHeaderPolicyId"` // Response Header策略
AccessLog string `field:"accessLog"` // 访问日志配置 AccessLog string `field:"accessLog"` // 访问日志配置
Stat string `field:"stat"` // 统计配置 Stat string `field:"stat"` // 统计配置
Gzip string `field:"gzip"` // Gzip配置
Cache string `field:"cache"` // 缓存配置
} }
type HTTPWebOperator struct { type HTTPWebOperator struct {
@@ -35,20 +33,18 @@ type HTTPWebOperator struct {
State interface{} // 状态 State interface{} // 状态
CreatedAt interface{} // 创建时间 CreatedAt interface{} // 创建时间
Root interface{} // 资源根目录 Root interface{} // 资源根目录
GzipId interface{} // Gzip配置
Charset interface{} // 字符集 Charset interface{} // 字符集
Shutdown interface{} // 临时关闭页面配置 Shutdown interface{} // 临时关闭页面配置
Pages interface{} // 特殊页面 Pages interface{} // 特殊页面
FirewallId interface{} // WAF ID
CachePolicyId interface{} // 缓存策略ID
RedirectToHttps interface{} // 跳转到HTTPS设置 RedirectToHttps interface{} // 跳转到HTTPS设置
Indexes interface{} // 首页文件列表 Indexes interface{} // 首页文件列表
MaxRequestBodySize interface{} // 最大允许的请求内容尺寸 MaxRequestBodySize interface{} // 最大允许的请求内容尺寸
StatPolicyId interface{} // 统计策略ID
RequestHeaderPolicyId interface{} // Request Header策略ID RequestHeaderPolicyId interface{} // Request Header策略ID
ResponseHeaderPolicyId interface{} // Response Header策略 ResponseHeaderPolicyId interface{} // Response Header策略
AccessLog interface{} // 访问日志配置 AccessLog interface{} // 访问日志配置
Stat interface{} // 统计配置 Stat interface{} // 统计配置
Gzip interface{} // Gzip配置
Cache interface{} // 缓存配置
} }
func NewHTTPWebOperator() *HTTPWebOperator { func NewHTTPWebOperator() *HTTPWebOperator {

View 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
}

View File

@@ -47,7 +47,6 @@ func (this *HTTPWebService) FindEnabledHTTPWeb(ctx context.Context, req *pb.Find
result.Id = int64(web.Id) result.Id = int64(web.Id)
result.IsOn = web.IsOn == 1 result.IsOn = web.IsOn == 1
result.Root = web.Root result.Root = web.Root
result.GzipId = int64(web.GzipId)
result.Charset = web.Charset result.Charset = web.Charset
result.RequestHeaderPolicyId = int64(web.RequestHeaderPolicyId) result.RequestHeaderPolicyId = int64(web.RequestHeaderPolicyId)
result.ResponseHeaderPolicyId = int64(web.ResponseHeaderPolicyId) result.ResponseHeaderPolicyId = int64(web.ResponseHeaderPolicyId)
@@ -78,7 +77,7 @@ func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.Updat
return nil, err return nil, err
} }
err = models.SharedHTTPWebDAO.UpdateWebGzip(req.WebId, req.GzipId) err = models.SharedHTTPWebDAO.UpdateWebGzip(req.WebId, req.GzipJSON)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -192,3 +191,19 @@ func (this *HTTPWebService) UpdateHTTPStat(ctx context.Context, req *pb.UpdateHT
} }
return rpcutils.RPCUpdateSuccess() 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()
}