实现基本的匹配条件管理

This commit is contained in:
GoEdgeLab
2020-09-29 11:28:39 +08:00
parent 43de7e4678
commit ba2bbaff0b
19 changed files with 613 additions and 23 deletions

View File

@@ -0,0 +1,39 @@
package conds
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/conds/condutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
)
type AddCondPopupAction struct {
actionutils.ParentAction
}
func (this *AddCondPopupAction) Init() {
}
func (this *AddCondPopupAction) RunGet(params struct{}) {
this.Data["components"] = condutils.ReadAllAvailableCondTypes()
this.Show()
}
func (this *AddCondPopupAction) RunPost(params struct {
CondType string
CondJSON []byte
Must *actions.Must
}) {
condConfig := &shared.HTTPRequestCond{}
err := json.Unmarshal(params.CondJSON, condConfig)
if err != nil {
this.Fail("解析条件设置时发生了错误:" + err.Error())
}
condConfig.Type = params.CondType
this.Data["cond"] = condConfig
this.Success()
}

View File

@@ -0,0 +1,37 @@
package conds
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/conds/condutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
)
type AddGroupPopupAction struct {
actionutils.ParentAction
}
func (this *AddGroupPopupAction) Init() {
}
func (this *AddGroupPopupAction) RunGet(params struct{}) {
this.Data["components"] = condutils.ReadAllAvailableCondTypes()
this.Show()
}
func (this *AddGroupPopupAction) RunPost(params struct {
CondGroupJSON []byte
Must *actions.Must
}) {
groupConfig := &shared.HTTPRequestCondGroup{}
err := json.Unmarshal(params.CondGroupJSON, groupConfig)
if err != nil {
this.Fail("解析条件时发生错误:" + err.Error())
}
this.Data["group"] = groupConfig
this.Success()
}

View File

@@ -0,0 +1,48 @@
package condutils
import (
"encoding/json"
"github.com/iwind/TeaGo/Tea"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
"path/filepath"
)
type CondJSComponent struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Component string `json:"component"`
}
// 读取所有可用的条件
func ReadAllAvailableCondTypes() []*CondJSComponent {
result := []*CondJSComponent{}
dir := Tea.Root + "/web/"
if Tea.IsTesting() {
dir = filepath.Dir(Tea.Root) + "/web"
}
dir += "/public/js/conds/"
jsonFiles := files.NewFile(dir).List()
for _, file := range jsonFiles {
if file.Ext() == ".json" {
data, err := file.ReadAll()
if err != nil {
logs.Println("[COND]read data from json file: " + err.Error())
continue
}
c := []*CondJSComponent{}
err = json.Unmarshal(data, &c)
if err != nil {
logs.Println("[COND]decode json failed: " + err.Error())
continue
}
result = append(result, c...)
}
}
return result
}

View File

@@ -0,0 +1,7 @@
package condutils
import "testing"
func TestReadAllAvailableCondTypes(t *testing.T) {
t.Log(ReadAllAvailableCondTypes())
}

View File

@@ -0,0 +1,17 @@
package conds
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Prefix("/servers/server/settings/conds").
GetPost("/addGroupPopup", new(AddGroupPopupAction)).
GetPost("/addCondPopup", new(AddCondPopupAction)).
EndAll()
})
}

View File

@@ -66,11 +66,12 @@ func (this *IndexAction) RunGet(params struct {
}
func (this *IndexAction) RunPost(params struct {
WebId int64
GzipId int64
Level int
MinLength string
MaxLength string
WebId int64
GzipId int64
Level int
MinLength string
MaxLength string
CondGroupsJSON []byte
Must *actions.Must
}) {
@@ -98,10 +99,11 @@ func (this *IndexAction) RunPost(params struct {
if params.GzipId > 0 {
_, err := this.RPC().HTTPGzipRPC().UpdateHTTPGzip(this.AdminContext(), &pb.UpdateHTTPGzipRequest{
GzipId: params.GzipId,
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
GzipId: params.GzipId,
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
CondGroupsJSON: params.CondGroupsJSON,
})
if err != nil {
this.ErrorPage(err)
@@ -109,9 +111,10 @@ func (this *IndexAction) RunPost(params struct {
}
} else {
resp, err := this.RPC().HTTPGzipRPC().CreateHTTPGzip(this.AdminContext(), &pb.CreateHTTPGzipRequest{
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
Level: types.Int32(params.Level),
MinLength: minLength,
MaxLength: maxLength,
CondGroupsJSON: params.CondGroupsJSON,
})
if err != nil {
this.ErrorPage(err)

View File

@@ -1,7 +1,8 @@
package ui
import (
"bytes"
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/conds/condutils"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/files"
@@ -21,7 +22,6 @@ func (this *ComponentsAction) RunGet(params struct{}) {
}
f := files.NewFile(webRoot)
buf := bytes.NewBuffer([]byte{})
f.Range(func(file *files.File) {
if !file.IsFile() {
return
@@ -34,9 +34,17 @@ func (this *ComponentsAction) RunGet(params struct{}) {
logs.Error(err)
return
}
buf.Write(data)
buf.WriteByte('\n')
buf.WriteByte('\n')
this.Write(data)
this.Write([]byte{'\n', '\n'})
})
this.Write(buf.Bytes())
// 条件组件
typesJSON, err := json.Marshal(condutils.ReadAllAvailableCondTypes())
if err != nil {
logs.Println("ComponentsAction: " + err.Error())
} else {
this.WriteString("window.REQUEST_COND_COMPONENTS = ")
this.Write(typesJSON)
this.Write([]byte{'\n', '\n'})
}
}

View File

@@ -30,6 +30,7 @@ import (
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/accessLog"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/cache"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/charset"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/conds"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/gzip"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/headers"
_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/http"