refactor code for add beego type router
This commit is contained in:
parent
5b46a08b79
commit
0429838598
@ -455,13 +455,18 @@ func (p *ControllerRegister) RouterAny(pattern string, f interface{}) {
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// AddRouterMethod("get","/api/:id", MyController.Ping)
|
// AddRouterMethod("get","/api/:id", MyController.Ping)
|
||||||
func (p *ControllerRegister) AddRouterMethod(method, pattern string, f interface{}) {
|
func (p *ControllerRegister) AddRouterMethod(httpMethod, pattern string, f interface{}) {
|
||||||
method = strings.ToUpper(method)
|
httpMethod = strings.ToUpper(httpMethod)
|
||||||
if method != "*" && !HTTPMETHOD[method] {
|
if httpMethod != "*" && !HTTPMETHOD[httpMethod] {
|
||||||
panic("not support http method: " + method)
|
panic("not support http method: " + httpMethod)
|
||||||
}
|
}
|
||||||
|
|
||||||
ct, methodName := getReflectTypeAndMethod(f)
|
ct, methodName := getReflectTypeAndMethod(f)
|
||||||
|
p.addBeegoTypeRouter(ct, methodName, httpMethod, pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// addBeegoTypeRouter add beego type router
|
||||||
|
func (p *ControllerRegister) addBeegoTypeRouter(ct reflect.Type, ctMethod, httpMethod, pattern string) {
|
||||||
route := &ControllerInfo{}
|
route := &ControllerInfo{}
|
||||||
route.pattern = pattern
|
route.pattern = pattern
|
||||||
route.routerType = routerTypeBeego
|
route.routerType = routerTypeBeego
|
||||||
@ -469,18 +474,16 @@ func (p *ControllerRegister) AddRouterMethod(method, pattern string, f interface
|
|||||||
route.controllerType = ct
|
route.controllerType = ct
|
||||||
|
|
||||||
methods := make(map[string]string)
|
methods := make(map[string]string)
|
||||||
if method == "*" {
|
if httpMethod == "*" {
|
||||||
for val := range HTTPMETHOD {
|
for val := range HTTPMETHOD {
|
||||||
methods[val] = methodName
|
methods[val] = ctMethod
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
methods[method] = methodName
|
methods[httpMethod] = ctMethod
|
||||||
}
|
}
|
||||||
route.methods = methods
|
route.methods = methods
|
||||||
|
|
||||||
for method := range methods {
|
p.addRouterForMethod(route)
|
||||||
p.addToRouter(method, pattern, route)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get reflect controller type and method by controller method expression
|
// get reflect controller type and method by controller method expression
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user