You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
47 lines
1.1 KiB
|
1 year ago
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"reflect"
|
||
|
|
"server/call"
|
||
|
|
"server/common"
|
||
|
|
"server/config"
|
||
|
|
"server/util"
|
||
|
|
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
)
|
||
|
|
|
||
|
|
func ParseUser(name string) (string, string) {
|
||
|
|
first := name[0]
|
||
|
|
// uid或者token(token全是大写)判断是哪个服
|
||
|
|
if (first >= 48 && first <= 57) || (first >= 65 && first <= 90) {
|
||
|
|
return "a", name
|
||
|
|
}
|
||
|
|
return name[0:1], name[1:]
|
||
|
|
}
|
||
|
|
|
||
|
|
// ShouldRoute 是否需要转发到其他服
|
||
|
|
func (g *Gin) ShouldRoute(req interface{}, fieldName string, apiType int) bool {
|
||
|
|
ref := reflect.ValueOf(req).Elem().FieldByName(fieldName)
|
||
|
|
name := ref.String()
|
||
|
|
flag, userName := ParseUser(name)
|
||
|
|
log.Debug("flag:%v,userName:%v", flag, userName)
|
||
|
|
if config.GetBase().ServerFlag == flag {
|
||
|
|
if name != userName {
|
||
|
|
ref.SetString(userName)
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
server := call.GetConfigServerFlag(flag)
|
||
|
|
if server == nil {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
apiURL := fmt.Sprintf("%s%s", server.APIURL, g.Context.Request.URL.Path)
|
||
|
|
if apiType == common.ProviderAPITypeJson {
|
||
|
|
util.HttpPost(apiURL, req, g.RetData, nil)
|
||
|
|
} else {
|
||
|
|
util.HttpPostForm(apiURL, req, g.RetData, nil)
|
||
|
|
}
|
||
|
|
return true
|
||
|
|
}
|