|
@@ -6,10 +6,13 @@ import (
|
|
|
. "gateway/common/gatecode"
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
+ "log"
|
|
|
+ "regexp"
|
|
|
)
|
|
|
|
|
|
type Manager struct {
|
|
|
- routers map[string]Router
|
|
|
+ eqRouters map[string]*Router
|
|
|
+ regexRouter map[*regexp.Regexp]*Router
|
|
|
}
|
|
|
|
|
|
// InitRouterManager 初始化系统代理路由
|
|
@@ -19,23 +22,34 @@ func InitRouterManager() (*Manager, error) {
|
|
|
return nil, fmt.Errorf("未发现可用路由")
|
|
|
}
|
|
|
routerManager := &Manager{
|
|
|
- routers: make(map[string]Router),
|
|
|
+ eqRouters: make(map[string]*Router),
|
|
|
+ regexRouter: make(map[*regexp.Regexp]*Router),
|
|
|
}
|
|
|
for _, row := range *res {
|
|
|
- if url := gconv.String(row["url"]); url != "" {
|
|
|
- routerManager.routers[url] = Router{
|
|
|
- Status: gconv.Int(row["status"]),
|
|
|
- PowerCheck: gconv.Int(row["ischeck"]),
|
|
|
- AccountCheck: gconv.Int(row["ischeck_status"]),
|
|
|
- AuthCheck: gconv.Int(row["ischeck_auth"]),
|
|
|
- BlackCheck: gconv.Int(row["ischeck_blacklist"]) == 1,
|
|
|
- Deduct: gconv.Int(row["deduct_source"]),
|
|
|
- FuncCode: gconv.String(row["function_code"]),
|
|
|
- MiddleCode: gconv.String(row["middleground_code"]),
|
|
|
- ReqUrl: url,
|
|
|
- TimeOut: gconv.Int64(row["timeout"]),
|
|
|
- Remark: gconv.String(row["remark"]),
|
|
|
+ router := gconv.String(row["url"])
|
|
|
+
|
|
|
+ routerRule := &Router{
|
|
|
+ Status: gconv.Int(row["status"]),
|
|
|
+ PowerCheck: gconv.Int(row["ischeck"]),
|
|
|
+ AccountCheck: gconv.Int(row["ischeck_status"]),
|
|
|
+ AuthCheck: gconv.Int(row["ischeck_auth"]),
|
|
|
+ BlackCheck: gconv.Int(row["ischeck_blacklist"]) == 1,
|
|
|
+ Deduct: gconv.Int(row["deduct_source"]),
|
|
|
+ FuncCode: gconv.String(row["function_code"]),
|
|
|
+ MiddleCode: gconv.String(row["middleground_code"]),
|
|
|
+ ReqUrl: router,
|
|
|
+ TimeOut: gconv.Int64(row["timeout"]),
|
|
|
+ Remark: gconv.String(row["remark"]),
|
|
|
+ }
|
|
|
+
|
|
|
+ if regexp.QuoteMeta(router) == router {
|
|
|
+ routerManager.eqRouters[router] = routerRule
|
|
|
+ } else {
|
|
|
+ reg, err := regexp.Compile(router)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("路由%s装载异常 %v\n", router, err)
|
|
|
}
|
|
|
+ routerManager.regexRouter[reg] = routerRule
|
|
|
}
|
|
|
}
|
|
|
return routerManager, nil
|
|
@@ -43,15 +57,22 @@ func InitRouterManager() (*Manager, error) {
|
|
|
|
|
|
// GetRouterRule 获取路由规则
|
|
|
func (m *Manager) GetRouterRule(url string) (*Router, error) {
|
|
|
- // WillDo:后续需要增加正则匹配
|
|
|
- rule, exists := m.routers[url]
|
|
|
+ routerRule, exists := m.eqRouters[url]
|
|
|
if !exists {
|
|
|
+ for reg, thisRouterRule := range m.regexRouter {
|
|
|
+ if reg.MatchString(url) {
|
|
|
+ routerRule = thisRouterRule
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if routerRule == nil {
|
|
|
return nil, NewErrorWithCode(GATEWAY_ROUTER_NOTFIND, fmt.Sprintf("未找到请求地址%s,请检查是否注册到数据库\n", url))
|
|
|
}
|
|
|
- if rule.Status != 1 {
|
|
|
- return nil, NewErrorWithCode(GATEWAY_ROUTER_UPHOLD, fmt.Sprintf("接口状态:%d\n", rule.Status))
|
|
|
+
|
|
|
+ if routerRule.Status != 1 {
|
|
|
+ return nil, NewErrorWithCode(GATEWAY_ROUTER_UPHOLD, fmt.Sprintf("接口状态:%d\n", routerRule.Status))
|
|
|
}
|
|
|
- return &rule, nil
|
|
|
+ return routerRule, nil
|
|
|
}
|
|
|
|
|
|
// InfusionContext 注入通用结构体gContext
|