路由规则找不到的时候提示用户

This commit is contained in:
GoEdgeLab
2023-07-17 15:30:19 +08:00
parent 35e94d40a4
commit ceddc34eb0
6 changed files with 36 additions and 7 deletions

View File

@@ -3,6 +3,7 @@
package utils
import (
"bytes"
"encoding/json"
"reflect"
)
@@ -22,3 +23,8 @@ func JSONClone(v interface{}) (interface{}, error) {
return nv, nil
}
// 判断JSON数据是否为null
func JSONIsNull(jsonData []byte) bool {
return len(jsonData) == 0 || bytes.Equal(jsonData, []byte("null"))
}

View File

@@ -4,6 +4,7 @@ package utils_test
import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/iwind/TeaGo/assert"
"testing"
)
@@ -23,3 +24,12 @@ func TestJSONClone(t *testing.T) {
t.Logf("%p, %#v", c, c)
}
}
func TestJSONIsNull(t *testing.T) {
var a = assert.NewAssertion(t)
a.IsTrue(utils.JSONIsNull(nil))
a.IsTrue(utils.JSONIsNull([]byte{}))
a.IsTrue(utils.JSONIsNull([]byte("null")))
a.IsFalse(utils.JSONIsNull([]byte{1, 2, 3}))
}

View File

@@ -28,7 +28,7 @@ func (this *IndexAction) RunGet(params struct {
}
this.Data["webId"] = webConfig.Id
locationMaps := []maps.Map{}
var locationMaps = []maps.Map{}
if webConfig.Locations != nil {
for _, location := range webConfig.Locations {
err := location.ExtractPattern()
@@ -46,7 +46,7 @@ func (this *IndexAction) RunGet(params struct {
this.ErrorPage(err)
return
}
pieces := strings.Split(location.Pattern, " ")
var pieces = strings.Split(location.Pattern, " ")
if len(pieces) == 2 {
m["pattern"] = pieces[1]
m["patternTypeName"] = serverconfigs.FindLocationPatternTypeName(location.PatternType())

View File

@@ -23,7 +23,12 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct {
LocationId int64
}) {
locationConfig := this.Data.Get("locationConfig").(*serverconfigs.HTTPLocationConfig)
var location = this.Data.Get("locationConfig")
if location == nil {
this.NotFound("location", params.LocationId)
return
}
var locationConfig = location.(*serverconfigs.HTTPLocationConfig)
this.Data["patternTypes"] = serverconfigs.AllLocationPatternTypes()

View File

@@ -20,13 +20,13 @@ func NewLocationHelper() *LocationHelper {
}
func (this *LocationHelper) BeforeAction(actionPtr actions.ActionWrapper) {
action := actionPtr.Object()
var action = actionPtr.Object()
if action.Request.Method != http.MethodGet {
return
}
serverIdString := action.ParamString("serverId")
locationIdString := action.ParamString("locationId")
var serverIdString = action.ParamString("serverId")
var locationIdString = action.ParamString("locationId")
action.Data["leftMenuItemIsDisabled"] = true
action.Data["mainMenu"] = "server"
@@ -39,7 +39,7 @@ func (this *LocationHelper) BeforeAction(actionPtr actions.ActionWrapper) {
if parentActionValue.IsValid() {
parentAction, isOk := parentActionValue.Interface().(actionutils.ParentAction)
if isOk {
locationId := action.ParamInt64("locationId")
var locationId = action.ParamInt64("locationId")
locationConfig, isOk := FindLocationConfig(&parentAction, locationId)
if !isOk {
return

View File

@@ -2,9 +2,12 @@ package locationutils
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/types"
)
// FindLocationConfig 查找路由规则配置
@@ -15,6 +18,11 @@ func FindLocationConfig(parentAction *actionutils.ParentAction, locationId int64
return
}
if utils.JSONIsNull(locationConfigResp.LocationJSON) {
parentAction.ErrorPage(errors.New("location '" + types.String(locationId) + "' not found"))
return
}
locationConfig = &serverconfigs.HTTPLocationConfig{}
err = json.Unmarshal(locationConfigResp.LocationJSON, locationConfig)
if err != nil {