Files
EdgeAdmin/internal/web/actions/default/servers/server/settings/gzip/index.go

154 lines
3.3 KiB
Go
Raw Normal View History

2020-08-21 21:09:42 +08:00
package gzip
import (
2020-09-16 09:09:10 +08:00
"encoding/json"
2020-11-20 15:32:42 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
2020-08-21 21:09:42 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2021-01-03 20:25:15 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
2020-09-16 09:09:10 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
2020-09-20 16:28:16 +08:00
"github.com/iwind/TeaGo/types"
2020-08-21 21:09:42 +08:00
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("gzip")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
2021-01-03 20:25:15 +08:00
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
2020-09-16 20:29:13 +08:00
if err != nil {
this.ErrorPage(err)
2020-09-16 09:09:10 +08:00
return
}
2020-09-23 18:43:38 +08:00
2020-09-16 20:29:13 +08:00
this.Data["webId"] = webConfig.Id
2020-09-16 09:09:10 +08:00
2020-09-20 16:28:16 +08:00
gzipId := int64(0)
if webConfig.GzipRef != nil {
gzipId = webConfig.GzipRef.GzipId
}
2020-09-16 09:09:10 +08:00
gzipConfig := &serverconfigs.HTTPGzipConfig{
Id: 0,
IsOn: true,
}
if gzipId > 0 {
resp, err := this.RPC().HTTPGzipRPC().FindEnabledHTTPGzipConfig(this.AdminContext(), &pb.FindEnabledGzipConfigRequest{HttpGzipId: gzipId})
2020-09-16 09:09:10 +08:00
if err != nil {
this.ErrorPage(err)
return
}
err = json.Unmarshal(resp.HttpGzipJSON, gzipConfig)
2020-09-16 09:09:10 +08:00
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["gzipConfig"] = gzipConfig
2020-08-21 21:09:42 +08:00
2020-09-23 18:43:38 +08:00
if webConfig.GzipRef == nil {
webConfig.GzipRef = &serverconfigs.HTTPGzipRef{
IsPrior: false,
IsOn: false,
GzipId: 0,
}
}
this.Data["gzipRef"] = webConfig.GzipRef
2020-08-21 21:09:42 +08:00
this.Show()
}
2020-09-16 09:09:10 +08:00
func (this *IndexAction) RunPost(params struct {
2020-10-05 16:54:34 +08:00
WebId int64
GzipId int64
Level int
MinLength string
MaxLength string
CondsJSON []byte
GzipRefJSON []byte
2020-09-16 09:09:10 +08:00
Must *actions.Must
}) {
2020-11-20 15:32:42 +08:00
// 日志
defer this.CreateLog(oplogs.LevelInfo, "修改Web %d 的GZip配置", params.WebId)
2020-09-16 09:09:10 +08:00
if params.Level < 0 || params.Level > 9 {
this.Fail("请选择正确的压缩级别")
}
minLength := &pb.SizeCapacity{Count: -1}
if len(params.MinLength) > 0 {
err := json.Unmarshal([]byte(params.MinLength), minLength)
if err != nil {
this.ErrorPage(err)
return
}
}
maxLength := &pb.SizeCapacity{Count: -1}
if len(params.MaxLength) > 0 {
err := json.Unmarshal([]byte(params.MaxLength), maxLength)
if err != nil {
this.ErrorPage(err)
return
}
}
2020-10-05 16:54:34 +08:00
gzipRef := &serverconfigs.HTTPGzipRef{}
err := json.Unmarshal(params.GzipRefJSON, gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
2020-09-16 09:09:10 +08:00
if params.GzipId > 0 {
_, err := this.RPC().HTTPGzipRPC().UpdateHTTPGzip(this.AdminContext(), &pb.UpdateHTTPGzipRequest{
HttpGzipId: params.GzipId,
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
CondsJSON: params.CondsJSON,
2020-09-16 09:09:10 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
} else {
resp, err := this.RPC().HTTPGzipRPC().CreateHTTPGzip(this.AdminContext(), &pb.CreateHTTPGzipRequest{
2020-09-29 17:23:06 +08:00
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
CondsJSON: params.CondsJSON,
2020-09-16 09:09:10 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
gzipRef.GzipId = resp.HttpGzipId
2020-10-05 16:54:34 +08:00
}
2020-09-16 09:09:10 +08:00
2020-10-05 16:54:34 +08:00
gzipRefJSON, err := json.Marshal(gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
2020-09-20 16:28:16 +08:00
2020-10-05 16:54:34 +08:00
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{
WebId: params.WebId,
GzipJSON: gzipRefJSON,
})
if err != nil {
this.ErrorPage(err)
2020-09-16 09:09:10 +08:00
}
this.Success()
}