集群可以单独设置WebP策略

This commit is contained in:
GoEdgeLab
2022-04-01 16:42:08 +08:00
parent 67602604dd
commit e21587b279
9 changed files with 188 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/cluster/settings/thresholds"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/cluster/settings/toa"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/cluster/settings/waf"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/cluster/settings/webp"
clusters "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/clusterutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
@@ -74,6 +75,10 @@ func init() {
GetPost("/createPopup", new(metrics.CreatePopupAction)).
Post("/delete", new(metrics.DeleteAction)).
// WebP
Prefix("/clusters/cluster/settings/webp").
GetPost("", new(webp.IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,98 @@
package webp
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "")
this.SecondMenu("webp")
}
func (this *IndexAction) RunGet(params struct {
ClusterId int64
}) {
webpResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeClusterWebPPolicy(this.AdminContext(), &pb.FindEnabledNodeClusterWebPPolicyRequest{NodeClusterId: params.ClusterId})
if err != nil {
this.ErrorPage(err)
return
}
if len(webpResp.WebpPolicyJSON) == 0 {
this.Data["webpPolicy"] = nodeconfigs.DefaultWebPImagePolicy
} else {
var config = &nodeconfigs.WebPImagePolicy{}
err = json.Unmarshal(webpResp.WebpPolicyJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webpPolicy"] = config
}
this.Show()
}
func (this *IndexAction) RunPost(params struct {
ClusterId int64
IsOn bool
RequireCache bool
MinLengthJSON []byte
MaxLengthJSON []byte
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo("修改集群 %d 的WebP设置", params.ClusterId)
var config = &nodeconfigs.WebPImagePolicy{
IsOn: params.IsOn,
RequireCache: params.RequireCache,
}
if len(params.MinLengthJSON) > 0 {
var minLength = &shared.SizeCapacity{}
err := json.Unmarshal(params.MinLengthJSON, minLength)
if err != nil {
this.ErrorPage(err)
return
}
config.MinLength = minLength
}
if len(params.MaxLengthJSON) > 0 {
var maxLength = &shared.SizeCapacity{}
err := json.Unmarshal(params.MaxLengthJSON, maxLength)
if err != nil {
this.ErrorPage(err)
return
}
config.MaxLength = maxLength
}
configJSON, err := json.Marshal(config)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().NodeClusterRPC().UpdateNodeClusterWebPPolicy(this.AdminContext(), &pb.UpdateNodeClusterWebPPolicyRequest{
NodeClusterId: params.ClusterId,
WebpPolicyJSON: configJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -13,7 +13,7 @@ import (
"strconv"
)
// 单个集群的帮助
// ClusterHelper 单个集群的帮助
type ClusterHelper struct {
}
@@ -124,6 +124,12 @@ func (this *ClusterHelper) createSettingMenu(cluster *pb.NodeCluster, info *pb.F
"isActive": selectedItem == "dns",
"isOn": cluster.DnsDomainId > 0 || len(cluster.DnsName) > 0,
})
items = append(items, maps.Map{
"name": "WebP",
"url": "/clusters/cluster/settings/webp?clusterId=" + clusterId,
"isActive": selectedItem == "webp",
"isOn": info != nil && info.WebpIsOn,
})
items = append(items, maps.Map{
"name": "统计指标",
"url": "/clusters/cluster/settings/metrics?clusterId=" + clusterId,

View File

@@ -5,6 +5,7 @@ package webp
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -44,6 +45,25 @@ func (this *IndexAction) RunGet(params struct {
this.Data["webId"] = webConfig.Id
this.Data["webpConfig"] = webConfig.WebP
// WebP策略配置
var serverMap = this.Data.GetMap("server")
var clusterId = serverMap.GetInt64("clusterId")
webpPolicyResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeClusterWebPPolicy(this.AdminContext(), &pb.FindEnabledNodeClusterWebPPolicyRequest{NodeClusterId: clusterId})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["requireCache"] = true
if len(webpPolicyResp.WebpPolicyJSON) > 0 {
var webpPolicy = &nodeconfigs.WebPImagePolicy{}
err = json.Unmarshal(webpPolicyResp.WebpPolicyJSON, webpPolicy)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["requireCache"] = webpPolicy.RequireCache
}
this.Show()
}

View File

@@ -62,7 +62,7 @@ func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) {
logs.Error(err)
return
}
server := serverResp.Server
var server = serverResp.Server
if server == nil {
logs.Error(errors.New("can not find the server"))
return
@@ -70,11 +70,18 @@ func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) {
// 初始化数据
if !action.Data.Has("server") {
action.Data["server"] = maps.Map{"id": server.Id, "name": server.Name}
if server.NodeCluster == nil {
server.NodeCluster = &pb.NodeCluster{Id: 0}
}
action.Data["server"] = maps.Map{
"id": server.Id,
"name": server.Name,
"clusterId": server.NodeCluster.Id,
}
}
// 服务管理
serverConfig := &serverconfigs.ServerConfig{}
var serverConfig = &serverconfigs.ServerConfig{}
err = json.Unmarshal(server.Config, serverConfig)
if err != nil {
logs.Error(err)

View File

@@ -1,5 +1,5 @@
Vue.component("http-webp-config-box", {
props: ["v-webp-config", "v-is-location", "v-is-group"],
props: ["v-webp-config", "v-is-location", "v-is-group", "v-require-cache"],
data: function () {
let config = this.vWebpConfig
if (config == null) {
@@ -75,7 +75,7 @@ Vue.component("http-webp-config-box", {
<input type="checkbox" value="1" v-model="config.isOn"/>
<label></label>
</div>
<p class="comment">选中后表示开启自动WebP压缩只有满足缓存条件的图片内容才会被转换。</p>
<p class="comment">选中后表示开启自动WebP压缩<span v-if="vRequireCache">;只有满足缓存条件的图片内容才会被转换</span>。</p>
</td>
</tr>
</tbody>

View File

@@ -0,0 +1,42 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
<form class="ui form" method="post" data-tea-action="$" data-tea-success="success">
<csrf-token></csrf-token>
<input type="hidden" name="clusterId" :value="clusterId"/>
<table class="ui table definition selectable">
<tr>
<td class="title">启用</td>
<td>
<checkbox name="isOn" v-model="webpPolicy.isOn"></checkbox>
<p class="comment">选中后表示当前集群下的服务可以使用WebP转换功能。</p>
</td>
</tr>
<tbody v-show="webpPolicy.isOn">
<tr>
<td>需要满足缓存条件</td>
<td>
<checkbox name="requireCache" v-model="webpPolicy.requireCache"></checkbox>
<p class="comment">选中后表示图片URL需要满足缓存条件后才会转换防止实时转换消耗巨大的系统资源。</p>
</td>
</tr>
<tr>
<td>可转换的最小尺寸</td>
<td>
<size-capacity-box :v-name="'minLengthJSON'" :v-value="webpPolicy.minLength"></size-capacity-box>
<p class="comment">不低于此尺寸的图像文件的才会转换。</p>
</td>
</tr>
<tr>
<td>可转换的最大尺寸</td>
<td>
<size-capacity-box :v-name="'maxLengthJSON'" :v-value="webpPolicy.maxLength"></size-capacity-box>
<p class="comment">不高于此尺寸的图像文件的才会转换。</p>
</td>
</tr>
</tbody>
</table>
<submit-btn></submit-btn>
</form>
</div>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
})

View File

@@ -13,7 +13,7 @@
<csrf-token></csrf-token>
<input type="hidden" name="webId" :value="webId"/>
<http-webp-config-box :v-webp-config="webpConfig"></http-webp-config-box>
<http-webp-config-box :v-webp-config="webpConfig" :v-require-cache="requireCache"></http-webp-config-box>
<submit-btn></submit-btn>
</form>