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

143 lines
3.1 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-08-21 21:09:42 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2020-09-21 19:51:50 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
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
}) {
2020-09-21 19:51:50 +08:00
webConfig, err := webutils.FindWebConfigWithServerId(this.Parent(), 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{GzipId: gzipId})
if err != nil {
this.ErrorPage(err)
return
}
2020-09-21 19:51:50 +08:00
err = json.Unmarshal(resp.GzipJSON, 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 {
WebId int64
GzipId int64
Level int
MinLength string
MaxLength string
Must *actions.Must
}) {
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
}
}
if params.GzipId > 0 {
_, err := this.RPC().HTTPGzipRPC().UpdateHTTPGzip(this.AdminContext(), &pb.UpdateHTTPGzipRequest{
GzipId: params.GzipId,
2020-09-20 16:28:16 +08:00
Level: types.Int32(params.Level),
2020-09-16 09:09:10 +08:00
MinLength: minLength,
MaxLength: maxLength,
})
if err != nil {
this.ErrorPage(err)
return
}
} else {
resp, err := this.RPC().HTTPGzipRPC().CreateHTTPGzip(this.AdminContext(), &pb.CreateHTTPGzipRequest{
2020-09-20 16:28:16 +08:00
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
2020-09-16 09:09:10 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
gzipId := resp.GzipId
2020-09-20 16:28:16 +08:00
gzipRef := &serverconfigs.HTTPGzipRef{
IsOn: true,
2020-09-16 09:09:10 +08:00
GzipId: gzipId,
2020-09-20 16:28:16 +08:00
}
gzipRefJSON, err := json.Marshal(gzipRef)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebGzip(this.AdminContext(), &pb.UpdateHTTPWebGzipRequest{
WebId: params.WebId,
GzipJSON: gzipRefJSON,
2020-09-16 09:09:10 +08:00
})
if err != nil {
this.ErrorPage(err)
}
}
this.Success()
}