mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 15:20:54 +00:00
Merge pull request #2656 from BorisBorshevsky/develop
Allow injecting dependencies to controllers
This commit is contained in:
commit
e14113aa0e
33
router.go
33
router.go
@ -117,6 +117,7 @@ type ControllerInfo struct {
|
|||||||
handler http.Handler
|
handler http.Handler
|
||||||
runFunction FilterFunc
|
runFunction FilterFunc
|
||||||
routerType int
|
routerType int
|
||||||
|
initialize func() ControllerInterface
|
||||||
methodParams []*param.MethodParam
|
methodParams []*param.MethodParam
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,6 +188,27 @@ func (p *ControllerRegister) addWithMethodParams(pattern string, c ControllerInt
|
|||||||
route.methods = methods
|
route.methods = methods
|
||||||
route.routerType = routerTypeBeego
|
route.routerType = routerTypeBeego
|
||||||
route.controllerType = t
|
route.controllerType = t
|
||||||
|
route.initialize = func() ControllerInterface {
|
||||||
|
vc := reflect.New(route.controllerType)
|
||||||
|
execController, ok := vc.Interface().(ControllerInterface)
|
||||||
|
if !ok {
|
||||||
|
panic("controller is not ControllerInterface")
|
||||||
|
}
|
||||||
|
|
||||||
|
elemVal := reflect.ValueOf(c).Elem()
|
||||||
|
elemType := reflect.TypeOf(c).Elem()
|
||||||
|
execElem := reflect.ValueOf(execController).Elem()
|
||||||
|
|
||||||
|
numOfFields := elemVal.NumField()
|
||||||
|
for i := 0; i < numOfFields; i++ {
|
||||||
|
fieldVal := elemVal.Field(i)
|
||||||
|
fieldType := elemType.Field(i)
|
||||||
|
execElem.FieldByName(fieldType.Name).Set(fieldVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
return execController
|
||||||
|
}
|
||||||
|
|
||||||
route.methodParams = methodParams
|
route.methodParams = methodParams
|
||||||
if len(methods) == 0 {
|
if len(methods) == 0 {
|
||||||
for _, m := range HTTPMETHOD {
|
for _, m := range HTTPMETHOD {
|
||||||
@ -768,14 +790,20 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
// also defined runRouter & runMethod from filter
|
// also defined runRouter & runMethod from filter
|
||||||
if !isRunnable {
|
if !isRunnable {
|
||||||
//Invoke the request handler
|
//Invoke the request handler
|
||||||
|
var execController ControllerInterface
|
||||||
|
if routerInfo.initialize != nil {
|
||||||
|
execController = routerInfo.initialize()
|
||||||
|
} else {
|
||||||
vc := reflect.New(runRouter)
|
vc := reflect.New(runRouter)
|
||||||
execController, ok := vc.Interface().(ControllerInterface)
|
var ok bool
|
||||||
|
execController, ok = vc.Interface().(ControllerInterface)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("controller is not ControllerInterface")
|
panic("controller is not ControllerInterface")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//call the controller init function
|
//call the controller init function
|
||||||
execController.Init(context, runRouter.Name(), runMethod, vc.Interface())
|
execController.Init(context, runRouter.Name(), runMethod, execController)
|
||||||
|
|
||||||
//call prepare function
|
//call prepare function
|
||||||
execController.Prepare()
|
execController.Prepare()
|
||||||
@ -810,6 +838,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
execController.Options()
|
execController.Options()
|
||||||
default:
|
default:
|
||||||
if !execController.HandlerFunc(runMethod) {
|
if !execController.HandlerFunc(runMethod) {
|
||||||
|
vc := reflect.ValueOf(execController)
|
||||||
method := vc.MethodByName(runMethod)
|
method := vc.MethodByName(runMethod)
|
||||||
in := param.ConvertParams(methodParams, method.Type(), context)
|
in := param.ConvertParams(methodParams, method.Type(), context)
|
||||||
out := method.Call(in)
|
out := method.Call(in)
|
||||||
|
Loading…
Reference in New Issue
Block a user